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
2 comments to "Polymorphism - Java - Examples static, dynamic, runtime - overloading - overriding"
Post a Comment
Whoever 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 doesn’t even require any sign-in, The good thing is that it can accept your lovely nick name also and the URL is not mandatory too.
Thanks for your patience
~Krishna(I love "Transparency")
Popular Posts
-
The best solution to know about these init levels is to understand the " man init " command output on Unix. There are basically 8...
-
How to Unlock BSNL 3G data card to use it with Airtel and Vodafone Model no : LW272 ? How to unlock BSNL 3G data card( Model no : LW272 )us...
-
How to transfer bike registration from one State to other (Karnataka to Andhra)?? Most of us having two wheelers purchased and registered in...
-
ఓం శ్రీ స్వామియే శరణం ఆయ్యప్ప!! Related posts : Trip to Sabarimala - Part 1 Trip to Sabarimala - Part 2 Ayappa Deeksha required things...
-
Following are some of interesting blogs I found till now ...please comment to add your blog here. Blogs in English : http://nitawriter.word...
Popular posts
- Airtel and vodafone GPRS settings for pocket PC phones
- Andhra 2 America
- Ayyappa Deeksha required things
- Blogs I watch !
- Captions for your bike
- DB2 FAQs
- Deepavali Vs The Goddes of sleep
- ETV - Dhee D2 D3
- Evolution of smoking in India Women
- How to make credit card payments?
- init 0, init 1, init 2 ..
- Java-J2EE interview preparation
- mCheck Application jar or jad download
- My SQL FAQs
- My Travelogues
- Old is blod - New is italic
- Online pay methids for credit cards
- Oracle FAQs
- Pilgrimages
- Smoking in Indian Women
- Technology Vs Humans
- Twitter feeds for all Telugu stars on single page.
- Unix best practices
- Unix FAQs
Abderrahmen Ben Mariem says:
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.
Abderrahmen Ben Mariem says:
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.