aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

Polymorphism

mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
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

Joined: Aug 31, 2004
Posts: 11343

This is a duplicate.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
Don't get me started about those stupid light bulbs.
 
subject: Polymorphism