As I already discussed in the last topic of object oriented programming (You can --> www.javabymanish.blogspot.in/). Now we are going to discussed the main concepts of OOP'S (Object Oriented Programming Syatem) :
This concepts (OOP'S) are base on real life and it has broadly four concepts :
1 - Abstraction
2 - Inheritance
3 - Encapsulation
4 - Polymorphism
These concepts are also known as Piller of OOP'S. Now we are going to explain one by one : -
1 - Abstraction :
Abstraction is one of the way to handle the complexity. Abstraction represents the essential properties and behaviors of an object and hide the complexity. A class's objects presents the properties and behaviors. The properties (or attributes) of an object are defined by fields in java and a fields in a class definition is a variable. The behaviors (or operations) of an object are defined by methods in java.
E.g.
If we are talking about the mobile then it is not necessary to know that how to mobile's functions are work for end-user, they only use the application. So in this case the functionality of an application is complex for end-user so ever end-user going to bought mobile they never ask the functionality, they only ask about the application that what applications are installed in the mobile.
2 - Inheritance :
Inheritance is the way to creating new class from existing ones. It means that the new class (or child class) are inherited common properties from existing class (parent class).
There are five types of inheritance but in java only three types are supported.
a) Single Inheritance :
This type of inheritance has one parent and one child class.
b) Multiple Inheritance :
This type of inheritance has two parent and one child class.
c) Multilevel Inheritance :
This type of inheritance has a level like grandparent, parent and child.
d) Hierarchical Inheritance :
This type of inheritance has one parent and more than one child.
e) Hybrid Inheritance :
This type of inheritance mixed all type of inheritance.
In java Multiple and Hybrid Inheritance does not support because these concepts are also not happen in real life. If it supported in java the problems come is:
In case of multiple inheritance, from which parent class the child class inherit the properties and hybrid contains multiple so hybrid also not use in java.
3 - Encapsulation :
Encapsulation means hiding the details from out side the class (or world). It make sense that to protect the properties and behaviors of an object of class. It means that security is the main purpose of encapsulation.
4 - Polymorphism :
In biology, Polymorphism means an organism can have many different forms.
Child class of a parent class can define their own behavior and yet share some of the same functionality of the parent class.
There are two types of Polymorphism :
a) Static Polymorphism
b) Dynamic Polymorphism
a) Static Polymorphism :
Static Polymorphism occur at compile time and always this concept occur in a single class. The concept of static polymorphism achieved by method overloading in java program.
Method Overloading :- In this concept we define a class that has at least two methods with the same name but having different parameter.
Ex.:
public class Overload
{
int c;
void divide(int a, int b)
{
c = a/b;
System.out.println("Result :"+c);
}
void divide(int x) // Overloaded method
{
System.out.println("The value of x = "+x);
}
public static void main(String args[])
{
Overload o1 = new Overload();
o1.divide(10,5);
o1.divide(20);
}
}
b) Dynamic Polymorphism :
Dynamic Polymorphism occur at run-time and for this concept we need at least two class. So this concept always occur in inheritance. We achieve dynamic polymorphism by method overriding.
Method Overriding :- In this concept we need two methods with same name, same parameter and same return type.
Ex. :
class Override
{
void display()
{
System.out.println("Method in parent class");
}
}
public class Override1 extends Override
{
void display()
{
System.out.println("Method in Child class"); //Overridden method
}
public static void main(String args[])
{
Override1 o1 = new Override1();
o1.display();
}
}
This concepts (OOP'S) are base on real life and it has broadly four concepts :
1 - Abstraction
2 - Inheritance
3 - Encapsulation
4 - Polymorphism
These concepts are also known as Piller of OOP'S. Now we are going to explain one by one : -
1 - Abstraction :
Abstraction is one of the way to handle the complexity. Abstraction represents the essential properties and behaviors of an object and hide the complexity. A class's objects presents the properties and behaviors. The properties (or attributes) of an object are defined by fields in java and a fields in a class definition is a variable. The behaviors (or operations) of an object are defined by methods in java.
E.g.
If we are talking about the mobile then it is not necessary to know that how to mobile's functions are work for end-user, they only use the application. So in this case the functionality of an application is complex for end-user so ever end-user going to bought mobile they never ask the functionality, they only ask about the application that what applications are installed in the mobile.
2 - Inheritance :
Inheritance is the way to creating new class from existing ones. It means that the new class (or child class) are inherited common properties from existing class (parent class).
There are five types of inheritance but in java only three types are supported.
a) Single Inheritance :
This type of inheritance has one parent and one child class.
b) Multiple Inheritance :
This type of inheritance has two parent and one child class.
c) Multilevel Inheritance :
This type of inheritance has a level like grandparent, parent and child.
d) Hierarchical Inheritance :
This type of inheritance has one parent and more than one child.
e) Hybrid Inheritance :
This type of inheritance mixed all type of inheritance.
In java Multiple and Hybrid Inheritance does not support because these concepts are also not happen in real life. If it supported in java the problems come is:
In case of multiple inheritance, from which parent class the child class inherit the properties and hybrid contains multiple so hybrid also not use in java.
3 - Encapsulation :
Encapsulation means hiding the details from out side the class (or world). It make sense that to protect the properties and behaviors of an object of class. It means that security is the main purpose of encapsulation.
4 - Polymorphism :
In biology, Polymorphism means an organism can have many different forms.
Child class of a parent class can define their own behavior and yet share some of the same functionality of the parent class.
There are two types of Polymorphism :
a) Static Polymorphism
b) Dynamic Polymorphism
a) Static Polymorphism :
Static Polymorphism occur at compile time and always this concept occur in a single class. The concept of static polymorphism achieved by method overloading in java program.
Method Overloading :- In this concept we define a class that has at least two methods with the same name but having different parameter.
Ex.:
public class Overload
{
int c;
void divide(int a, int b)
{
c = a/b;
System.out.println("Result :"+c);
}
void divide(int x) // Overloaded method
{
System.out.println("The value of x = "+x);
}
public static void main(String args[])
{
Overload o1 = new Overload();
o1.divide(10,5);
o1.divide(20);
}
}
b) Dynamic Polymorphism :
Dynamic Polymorphism occur at run-time and for this concept we need at least two class. So this concept always occur in inheritance. We achieve dynamic polymorphism by method overriding.
Method Overriding :- In this concept we need two methods with same name, same parameter and same return type.
Ex. :
class Override
{
void display()
{
System.out.println("Method in parent class");
}
}
public class Override1 extends Override
{
void display()
{
System.out.println("Method in Child class"); //Overridden method
}
public static void main(String args[])
{
Override1 o1 = new Override1();
o1.display();
}
}