5 Jun 2015

Why we use OOPS concepts in java

Object oriented programming is a concept that was created because of the need to overcome the problems that were found with using structured programming techniques. While structured programming uses an approach which is top down, OOP uses an approach which is bottom up. Traditionally, programming has placed an emphasis on logic and actions.

Object oriented programming has taken a completely different direction, and will place an emphasis on objects and information. With object oriented programming, a problem will be broken down into a number of units. These units are called objects. The foundation of OOP is the fact that it will place an emphasis on objects and classes.

Objects will be defined, and they will interact inside the system in a number of different ways. There are a number of advantages to be found with using the OOP paradigm, and some of these are simple maintenance, an advanced analysis of complicated programs, and re-usability.

There are a number of programming languages that use OOP, and some of these are Java, C++, and Ada. One concept that you will want to become familiar with is data modeling. Before you can construct an object oriented system, you will first need to find the objects within the system and determine the relationships they have. This process is called data modeling.

OOPs stands for Object Oriented Programming. The concepts in oops are similar to anyother programming languages. Basically, it is program agnostic. The different OOps concepts are :
Polymorphism
Inheritance
Abstraction
Encapsulation
Aggreagation
Composition
Association

Q1) What is polymorphism?
Ans) The abiltiy to define a function in multiple forms is called Polymorphism. In java, c++ there are two type of polymorphism: compile time polymorphism (overloading) and runtime polymorphism (overriding).
Mehtod overriding: Overriding occurs when a class method has the same name and signature as a method in parent class. When you override methods, JVM determines the proper methods to call at the program’s run time, not at the compile time.

Overloading: Overloading is determined at the compile time. It occurs when several methods have same names with:
Different method signature and different number or type of parameters.
Same method signature but different number of parameters.
Same method signature and same number of parameters but of different type
Example of Overloading

int add(int a,int b)
float add(float a,int b)
float add(int a ,float b)
void add(float a)
int add(int a)
void add(int a) //error conflict with the method int add(int a)
class BookDetails {
String title;
setBook(String title){}
}
class ScienceBook extends BookDetails{
setBook(String title){} //overriding
setBook(String title, String publisher,float price){} //overloading
}

Q2) What is inheritance?
Ans) Inheritance is the property which allows a Child class to inherit some properties from its parent class. In Java this is achieved by using extends keyword. Only properties with access modifier public and protected can be accessed in child class.

public class Parent {
public String parentName;
public String familyName;

protected void printMyName() {
System.out.println(“ My name is “+ chidName+” “ +familyName);
}
}
public class Child extends Parent {
public String childName;
public int childAge;
//inheritance
protected void printMyName() {
System.out.println(“ My child name is “+ chidName+” “ +familyName);
}
}
In above example the child has inherit its family name from the parent class just by inheriting the class. When child object is created printMyName() present in child class is called.

Q3) What is multiple inheritance and does java support?
Ans) If a child class inherits the property from multiple classes is known as multiple inheritance. Java does not allow to extend multiple classes, to overcome this problem it allows to implement multiple Interfaces.

Q4) What is an abstraction ?
Ans) Abstraction is a way of converting real world objects in terms of class. Its a concept of defining an idea in terms of classes or interface. For example creating a class Vehicle and injecting properties into it. E.g
public class Vehicle {
public String colour;
public String model;
}

Q5) What is Encapsulation?
Ans) The encapsulation is achieved by combining the methods and attribute into a class. The class acts like a container encapsulating the properties. The users are exposed mainly public methods.The idea behind is to hide how thinigs work and just exposing the requests a user can do.

Q6) What is Association?

Ans) Association is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Q7) What is Aggregation?
Ans) Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about "has-a" relationship.

Q8) What is Composition ?
Ans) Composition is again specialize form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object dose not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Everybody loves it when individuals meet up and share sees. Extraordinary blog, keep it up!tech updates

    ReplyDelete
  3. Thanks for the information. I really like the way you express complex topics in lucid way. It really helps me understand it much better way. best SEO company in USA

    ReplyDelete