Win a copy of Terraform in Action this week in the Cloud forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Polymorphism

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi, I am confused with POLYMORPHISM in overloaded and overriden methods. So, as I understood, Java doesn't give you polymorphism when you use overloading. For example,

class Animal{}
class Horse extends Animal{}
class TestIt{
public void eat(Animal a){
System.out.println("Animal is eating!!!");
}
public void eat(Horse h){
System.out.println("Horse is eating!!!");
}

public static void main(Stirng [] args){
Animal a = new Animal();
Animal h = new Horse();
TestIt t = new TestIt();
t.eat(a);// prints "Animal is eating!!!"
t.eat(h);// prints "Horse is eating!!!"
}
}

Can't understand why it does it? Why not to use polymorphism...

Next example:
class Animal{
public void eat(){
System.out.println("Animal is eating");
}

}
class Horse extends Animal{
public void eat(){
System.out.println("Horse is eating");
}
public void eat(String food){
System.out.println("Horse is eating " + food);
}
pulbic class Test{


}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is a duplicate.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic