• Post Reply 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Output........?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Test{
public static void main(String[] args){
Mammal h = new Horse();
Cattle c = new Horse();
c.eat(h);
}
}
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What confusion you have?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have tried this code and got output "Mammal eats food"

But my confusion is
1.c's type is cattle and is referring to instance of Horse.So which eat method should get called and why? what is the funda behind this.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be very clear with the concept of inheritance before solving the problem.You can think of class Cattle contains two overloaded version of eat method one takes Mammal object and other takes Cattle object.Since h is an instance of Mammal so the eat method which takes Mammal as an argument will get invoked at runtime.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sanjeev Singh
...takes Mammal as an argument will get invoked at runtime.



Sanjeev, shouldn�t it be �� takes Mammal as an argument passed at compile time?�

Richard - "With his K&B book open to page 109."
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Singh:
Since h is an instance of Mammal so the eat method which takes Mammal as an argument will get invoked at runtime.



HI Sanjeev,

I tried inserting System.out.println(h.getClass()); after c.eat(h) and it prints class horse.

Since eat method in Horse is an instance method and h is an instance of Horse, shouldn't c.eat(h) should print "Horse eats Hay"?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which type's method gets invoked at runtime is different from which type is being passed as the method argument.

Eventhough the horse class's method will be called at runtime, since the argument type is Mammal, it uses the method that takes Mammal argument which is present only in the base class. The following is a slightly modified version of the above code. I added another method that takes no argument. Hope this helps.



In lines 1 and 3, since there is no method argument and horse class does have a method with no argument, Horse class' method gets called.

One other way to test this is to add a method eat(Mammal m) to the Horse class and see that getting called.
reply
    Bookmark Topic Watch Topic
  • New Topic