File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism
 
Similar Threads
Unable to understand reason for ClassCastException
OverRiding a private method
Confused in function overriding
Polymorphism
overload and override method question