• Post Reply Bookmark Topic Watch Topic
  • New Topic

method invocation  RSS feed

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In the above given i thought line 1 would invoke eat() methode overided in class Cattle but it's invoked overridden eat() method of class Mammal and printed "Mammal eats food".
Some1 plz explain me why
thanx in advance
regds
Arpana
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Certainly eat( Mammal) should be invoked.
in class Cattle, u overload the method eat,
so ,in Cattle, u have two different methods:
one get a parameter which type is Mammal, the other get a Cattle.
when u invoke eat method, u pass a Mammal reference , exactly equals the method eat herit from super class Mammal. so print what u had get.
 
Keen Chen
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW:
u only OVERLOAD it,
not override.
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk....got the point.but at line 1 object of Horse class has been paased(though it is of Mammal Type), so the eat(Horse h) method oh Horse class should be invoked.

what do you say??
regds
Arpana
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes , I will also expect only eat(Horse h) method to be invoked . So the question trickles down to whether the object reference "h" that is passed as a argument to eat method is really a "Horse" or a "Mammal".
Meanwhile , I once asked question in other thread about Bill book on SCJP.
Object A = new Long(5);
Then will A.equals call equals method defined in Object class or Long class ?
The answer was it will call equals of Long class since A is pointing to a Long Object.
So both answers seems to be contradicting each other ...
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At compile time, the Java compiler chooses the method whose signature most closely matches the arguments of the call. It does not know the actual classes that will be assigned to the argument parameters at run-time, so it relies on the types of the arguments.
For example, in the line c.eat(h), c is of type Cattle, and h is of type Mammal, so the compiler will look for a method with a signature of eat(Mammal) in the Cattle class. It finds the method inherited from Mammal.
You will see the behavior you are expecting if you add this method to the Horse class, because you would then be overriding eat(Mammal):
void eat(Mammal m){
System.out.println("Horse eats hay");
}
As Keen said, eat() is overloaded, not overridden. In the code given, Horse has 3 overloaded versions of eat:
eat(Mammal)
eat(Cattle)
eat(Horse)
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exam also covers some basic OO concepts and contains questions about the proper use of inheritance. The code example used in the initial post of this thread is a good example of a sample program that would be accompanied by a question such as: "Does this program make appropriate use of inheritance?"
To answer the question you would ask yourself "Is it correct to say that a cow "is-a" mammal?" The answer is "yes" so it is proper for Cattle to extend the Mammal class. However, it is not correct to say that a Horse "is-a" cow. Therefore, it is not proper for Horse to extend Cattle. Instead, Horse should extend Mammal.
Some examples of proper Cattle subclasses might be Angus, Brahman, Highland, Texas Longhorn and many others.
For the exam you will need to remember that inheritance is an "is-a" relationship. Delegation is a "has-a" relationship.
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John you are right.When i made the changes in the method signature of eat() method of Mammal and Cattle class as shown below its shows the compilor err at line 1 that eat(Mammal) method is not resloved.

So, what i concluded is that in the case of overloading ,'type of object' play the key role in invoking method. But when all the methods have the same parameter(i,e in the case of overriding),
'object of class' decides which method to be invoked.That is why as Ram said "Object A=new Long(5), here A invokes equals() method of Long class" is true coz equals() of Object class is overrided in Long class
Correct me if i am wrong somewhere.
thanx
regds
Arpana
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arpana, whatever you mentioned about Overridden method is correct, but still a small point to note down.
"'object of class' decides which method to be invoked" -- This is true only for Non-Static methods...For Static methods the Type of the object will decide which method to invoke..
I hope I am right with the above point.
 
Arpana Rai
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ramki srini:
Arpana, whatever you mentioned about Overridden method is correct, but still a small point to note down.
"'object of class' decides which method to be invoked" -- This is true only for Non-Static methods...For Static methods the Type of the object will decide which method to invoke..
I hope I am right with the above point.


You are right Ramki.I forgot to mention it in my earlier post.Thanx for the addition.
regds
Arpana
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!