aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "output" Watch "output" New topic
Author

output

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
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

nss
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
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

Joined: Oct 11, 2001
Posts: 204
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

Joined: Sep 16, 2001
Posts: 139
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

Joined: Nov 07, 2001
Posts: 27
<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

Joined: Oct 11, 2001
Posts: 204
thanx Nain and Vinay
I got it now
Regards
Neha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: output
 
Similar Threads
Inheritance doubt
polymorphism
Doubt in the output of this program.
explain the flow of the program
Output........?