Polymorphism - Java - Examples static, dynamic, runtime - overloading - overriding

The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages. This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

Polymorphism can exists in two ways... 1. Static polymorphism - Overloading
2. Dynamic polymorphism  - Overriding

Overloading : Creating mutiple methods with same name in a class but with different signatures (either number of parameters should be different or type of parameters should be different in each method)
Overriding :  Creating same method same parameters but with different behavior in both super class and extended class is called as overriding.

Dynamic binding is nothing but choosing which method to chose when super class object is created with extended class type.
Please have a look at the below program and output to understand the polymorphisms...


public class Ploymorphism {

public interface Vegetarian{
void eatVeg();
}
public class Animal{
void eat(){
System.out.println("All animals can eat");
}
}
public class Deer extends Animal implements Vegetarian{

public void eat(String veg) {
System.out.println("I am eating :"+veg);
}
public void eatVeg(){
System.out.println("I eat only veg");
}

}
public class Tiger extends Animal{
void eat(){
System.out.println("I'm tiger, I eat what I want");
}
void eat(String veg, String nonveg){
System.out.println("I can eat:"+veg+" and "+nonveg);
}
}
public static void main(String[] args) {
System.out.println("Java has two types of polymorphism");
System.out.println("1.Static Polymorphism : Achived using \"overloading\" ");
Ploymorphism p = new Ploymorphism();
Tiger tiger = p.new Tiger();
tiger.eat("leaves", "creatures");
System.out.println("Tiger has two methods with same name but with different paramaetrs/ signatiures(either no of parameters should be different or type of paramters should be different)");
System.out.println("2.Dynamic Polymorphism : Achived using \"overriding\" ");
Deer deer = p.new Deer();
deer.eat("carrot");
System.out.println("deer is extended from animal but eat method has different in the deer than animal class- it is overriding");
Animal whiteTiger = p.new Tiger();
whiteTiger.eat();
//whiteTiger.eat("prawns","fish");
//System.out.println("The above commented line gives error as there is no eat method of such type in the animal class");

}

}

Ouput of the above program :

Java has two types of polymorphism
1.Static Polymorphism : Achived using "overloading"
I can eat:leaves and creatures
Tiger has two methods with same name but with different paramaetrs/ signatiures(either no of parameters should be different or type of paramters should be different)
2.Dynamic Polymorphism : Achived using "overriding"
I am eating :carrot
deer is extended from animal but eat method has different in the deer than animal class- it is overriding
I'm tiger, I eat what I want

 

Reference : [1] [2] [3]


2 comments to "Polymorphism - Java - Examples static, dynamic, runtime - overloading - overriding"

  • Hello sir,
    i came across your post about polymorphism in java.
    It was very helpful for me.
    I suggest also to add this tutorial as a reference:
    http://how-to-program-in-java.com/2016/08/17/polymorphism-java-examples-static-dynamic/

    It will help your readers for sure.
    Thanks in advance.

  • Thank you for the great tutorial.
    This one is interesting too:
    http://how-to-program-in-java.com/2016/08/17/polymorphism-java-examples-static-dynamic/
    I think that your readers will enjoy it.

Post a Comment

Who ever writes Inappropriate/Vulgar comments to context, generally want to be anonymous …So I hope U r not the one like that?
For lazy logs u can at least use Name/URL option which don’t even require any sign-in, good thing is that it can accept your lovely nick name also and URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
వీలయితే నాల్గు పోస్టులు ...కుదురితే ఒక కామెంటూ ...

Translate

Enter your email address:

Buffs ...

Visitors


hits counter
View My StatsCheck Google Page Rank

Add to Google Reader or Homepage

Bookmark and Share

Tags


Powered by WidgetsForFree