Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

output

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • 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);
}
}
output is Mammal eats food.Can someone tell me how
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neha Sawant:
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);
}
}
output is Mammal eats food.Can someone tell me how

Since you created h as a Mammal (of type Horse) when you call eat(h) you have to look for a method that will take an argument of 'Mammal' (since you in a sense casted Horse 'up' to a higher class ). When you call c.eat(h) you are now calling the methods inside the Horse class but since Horse extends Cattle which extends Mammal you have a copy of the void eat(Mammal m)method in Horse so this method is called which produces your result.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Rick,
If i comment out the method eat in Mammal then will i get compile time error.

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);
}
}
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha,

If i comment out the method eat in Mammal then will i get compile time error.

When an expression like c.eat(h) is compiled, java compiler
will search for all methods eat(h) that have the same
parameter type as h (i.e. Mammal) in class "c" (i.e Cattle)
and inherited methods in "c"'s superclass (i.e Mammal).
Since the only method eat() that has the right parameter type
was in Mammal which you have removed, compiler complained for
not being able to find an applicable method.
You can check JLS 15.12.1 & 2 for details.

[This message has been edited by Nain Hwu (edited November 12, 2001).]
 
vinay jain
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pre>
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);
}
}
</pre>
In the above example the method eat is not being overridden so the compiler matches the correct method i.e. Mammel::eat as h is of type Mammel, but if you change the code to
<pre>
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Mammal c){
System.out.println("Cattle eats hay");
}
}
class Horse extends Cattle{
void eat(Mammal 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);
}
}
</pre>
then the method Horse::eat will be called.
Vinay
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Nain and Vinay
I got it now
Regards
Neha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic