Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
mukhan myrzakulov
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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{


}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Report post to moderator
This is a duplicate.
 
    Bookmark Topic Watch Topic
  • New Topic