This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Inheritance doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
The output for the below code is "Mammal eats food". Can anybody explain me how?

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);
}
}

Source: TestCafe

Thanks All
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects of class Horse inherit the overloaded methods defined in the parent classes. So you can consider that an object of horse has the following methods:

void eat(Mammal m) // From Mammal
void eat(Cattle c){ // From Cattle
void eat(Horse h) // From Horse

So for the code that says:

Cattle c = new Horse();
c.eat(h);

you are basically using polymorphism to call a method in the Horse object, because at runtime the JVM knows that the Cattle reference is actually referring to a Horse object.

So you know that you have a choice of the above 3 methods to call. When the JVM decides to invoke an overloaded method, it uses the "reference type" being passed as an argument. In the method call "c.eat(h)", you are passing a variable refernce of type "Mammal", because of the following statement:

Mammal h = new Horse();

Polymorphism does not apply to choosing which overloaded method to call. The JVM will not look at what underlying object is connected to the variable reference, will only look at the variable reference type (h is a Mammal object reference).

So it will invoke the inherited "void eat(Mammal m)" method that prints out "Mammal eats food".

Hope this explains it. This is a tricky question that tests your knowledge of inheritence, overloading and the way the JVM chooses which method to invoke.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steven,
I have a doubt since the methods are overloaded the method called will be of variable reference type. In the below code why the method from cattle wont get called.


void eat(Mammal m) // From Mammal
void eat(Cattle c){ // From Cattle
void eat(Horse h) // From Horse

So for the code that says:

Mammal h= new Horse();
Cattle c = new Horse();
c.eat(h);


Thanks All
 
Steven Young
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For c.eat(h)

c is is a Cattle variable reference that points to an instance of Horse.
So the call to eat method will be in the Horse instance as per polymophism rules.

"h" is a Mammal object reference, and that is used to determine which mathod to invoke. It won't invoke the eat(Cattle c) method because the argument type is Cattle, not Mammal. Likewise, can't invoke eat(Horse h) because we are dealing with Mammal object reference "h".

So it's the inherited method eat(Mammal m) that is called.

Does that help or I'm I not explaining myself properly ?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:
Hi Steven,
I have a doubt since the methods are overloaded the method called will be of variable reference type. In the below code why the method from cattle wont get called.


void eat(Mammal m) // From Mammal
void eat(Cattle c){ // From Cattle
void eat(Horse h) // From Horse

So for the code that says:

Mammal h= new Horse();
Cattle c = new Horse();
c.eat(h);
Thanks All






#1- Ref var Mammal can hold ref to object of Mammal,Cattle and Horse
#2- Ref var Cattle can hold ref to object of Cattle and Horse
#3- Ref var of Horse can hold ref to Horse only

Cattle has two methods, own overloaded with Cattle and another inherited
from Mammal. One takes Cattle and inherited method takes Mammal.
When you pass ref of Mammal using ref variable of Cattle, eat(Cattle) can't
be called because, Mammal is parent of Cattle (Case #2 applies), so the
inherited method that is eat(Mammal) can be used and is the only that can be
used.

Anyway if you pass, Cattle ref to that, exact match eat(Cattle) will be
called.


Thanks,
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chandra and Steven for that good explanation
reply
    Bookmark Topic Watch Topic
  • New Topic