File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism" Watch "polymorphism" New topic


Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
class Horse extends Cattle{
void eat(Horse h){
System.out.println("Horse eats hay");
public class Mm{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();;
Why is the output
"Mammal eats food" ?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi there:
On the surface it looks like that eat() is over-
ridden in sub-classes. In fact it is not, as the
argument signature is differenct in each sub-class.
So method eat(Mammal m) is inherited in the cattle
class and it is called by c.
Hope this helps
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Each of your eat() methods has a different argument list, so you are not overriding, you are overloading.
Do you realize that each instance method automatically gets a variable called "this", which is a reference to the object that invoked it? You most likely don't really want to have an argument to eat() at all.

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
I am still a bit confused. Isn't 'c' an object of Horse so won't
'' call 'eat()' on horse ? and in the same vien isn't 'h' an object of horse ? Sorry i am getting a bit confused here
Thanx for the help
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Hi Soum,
The question's a bit tricky. What you said was actually my first answer.
At compile-time:
h -> Mammal
c -> Cattle
h -> Horse
c -> Horse
Since Java knows h to be an instance of Mammal at compile-time, no matter what the class of the object it holds at runtime, the method eat(Mammal) will be called.
Also, note that c, being declared as of class Cattle, cannot call eat(Horse). This is because the methods are overloaded, not overriden.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Soum:
Let us take from beginning. Method eat(Mammal M) is NOT over-ridden by Method eat(Cattle c) because
eat(Mammal M) has different argument than
eat(Cattle c). Therefore, eat(Mammal M) is
inheritated in sub-class Cattle. Same is the story
between eat(Cattle c) and eat(Horse h). Ultimately, eat(Mammal M) is inherited in Horse
class. Now is called. Note that H is of
Mammale type but holds Horse object. Because we
are passing Mammale type parameter, the
inherited eat(Mammal M) in c object is invoked.
Hence, it prints what it prints...
[ August 19, 2002: Message edited by: Barkat Mardhani ]
I agree. Here's the link:
subject: polymorphism
It's not a secret anymore!