The Most important basic concepts in OOPs(Object Oriented Programing) are Inheritance, Abstraction, Polymorphism and Encapsulation.
Poly means Many and Morph means Forms, Polymorphism is the capability of an object to take on many forms.
That means one object can perform different behavior on same time
Example a woman can be act as mother, sister, child at same time.
class Maths{
public void addition(){
System.out.println(" Math addition ");
}
}
class Calculator extends Maths{
@Override
public void addition(){
System.out.println(" Calculator addition ");
}
}
class Addition extends Maths{
@Override
public void addition(){
System.out.println(" Calculator addition ");
}
}
Here you can see both sub classes have same action, but there were different ways to execute these action, this is called polymorphism.Java Polymorphism Types
in java polymorphism categorized into two types
Static Polymorphism is also treat as Compile time Polymorphism.
This can be as of Static Binding,Early Binding and Method Overloading form.
Static Polymorphism can be achieved by Method Overloading.What is Method Overloading
A method having same name with different parameters(arguments) is nothing but Method Overloading
Example of Method Overloading
1) Same type with multiple arguments
class Addition{
void add(int a,int b)
{
System.out.println(a+b);
}
void add(int a,int b,int c)
{
System.out.println(a+b+c);
}
}
public class MyClass{
public static void main(String args[])
{
Addition addition=new Addition();
addition.add(1,2);// Method with 2 arguments output:3
addition.add(1,2,3);// Method with 3 arguments output:6
}
}
2) with different type
class Multiply{
void multiply(int a,int b)
{
System.out.println(a*b);
}
void multiply(float a,float b)
{
System.out.println(a*b);
}
}
public class MyClass{
public static void main(String args[])
{
Multiply mutiply=new Multiply();
mutiply.add(1,2);// Method with 2 arguments output:2
mutiply.add(1.0f,2.0f);// Method with 3 arguments output:2.0
}
}
Static polymorphism executes fast, why because the compiler will binds the methods on compile time itself.
Dynamic Polymorphism:
This is also known as Late Binding, Method Overriding.
What is Method Overriding?
A class inherits a method from its parent class is called Method Overriding.
Example:
class Animal{
public void giveMilk(){
}
}
class Cow extends Animal{
@override
public void giveMilk(){
}
}
Here the child class Cow contains giveMilk() method overridden from its parent class Animal.
Then what is Dynamic Polymorphism here, yes we are going to discuss about that.
lets create 3 classes
class Animal{
public void giveMilk(){
System.out.println("Animal give Milk");
}
}
class Cow extends Animal{
@override
public void giveMilk(){
System.out.println("Cow give Milk");
}
}
class Goat extends Animal{
@override
public void giveMilk(){
System.out.println("Goat give Milk");
}
}
class MyForm {
public static void main(String args[]){
Animal animal=new Animal();
Cow cow=new Cow();
Goat goat=new Goat();
animal.giveMilk();
cow.giveMilk();
goat.giveMilk();
}
}
<
Compiler at compile time treat
animal is the reference of Animal class and animal.giveMilk() will execute at runtime
output Animal give Milk??
cow is the reference of Cow class and cow.giveMilk() will execute at runtime
output Cow give Milk
goat is the reference of Goat class and goat.giveMilk() will execute at runtime
output Goat give Milk
Lets change the Object creation to
class MyForm {
public static void main(String args[]){
Animal animal1=new Cow();
Animal animal2=new Goat();
animal1.giveMilk();
animal2.giveMilk();
}
}
In this case
While compile time compiler treat
animal1 is the reference of Animal but at runtime JVM binds this method to Cow instance and animal1.giveMilk()
Output Cow give Milk
animal2 is the reference of Animal but at runtime JVM binds this method to Cow instance and animal2.giveMilk()
Output Cow give Milk
This is called Late Binding/Dynamic Polymorphism.
Lets check Difference Between Compile time Polymorphism & Run time Polymorphism
Run time Polymorphism |
Compile time Polymorphism |
Method binding done by compiler at compile time | Method binding done at runtime |
It is also know as Static binding, Method Overloading | It is also know Runtime binding, Method Overriding |
Overloading share the same name with different parameters | Overriding same method with same parameters or signature, but associated in a class & its subclass |
It is fast execution because known early at compile time | It is slow execution as compare to early binding because it is known at runtime |