• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Overloading

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody explain the flow of this program....

code
-------------------------------------
What is the result of compiling and running this program?

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


1. prints "Mammal eats food"
2. prints "Cattle eats hay"
3. prints "Horse eats hay"
4. Class cast Exception at runtime.

ANS : 1
--------------------------------------------
 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling the eat method with Mammal reference and the only match it finds is in the super class mammal.
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jothi...

My doubt is still not clear..
Mammal h = new Horse();
Here,h is a reference of type mammal but it is pointing to Horse.right?
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

ram



Here the reference type is cattle and in the cattle class there are two
eat() methods ,one has a parameter Mammal and one has cattle as arguments.
Now you are calling the eat() method with mammal reference.Now the compiler checks the refference,it finds two eat() methods ,and then checks the most specific one ,here the most specific one is mammal,so the eat method in the mammal class has executed.

AT compile time the compiler knows which method it has to execute and at run time the jvm runs that method.

Note:
The method which has to be executed is resolved at compile time depending on the refernce type ,if the method is OVERLOADED.
Thanks
Anil Kumar
[ June 04, 2007: Message edited by: anil kumar ]
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jothi...

My doubt is still not clear..

Mammal h = new Horse();
Here,h is a reference of type mammal but it is pointing to Horse.right?
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jothi...

My doubt is still not clear..

Mammal h = new Horse();
Here,h is a reference of type mammal but it is pointing to Horse.right?
 
Joe Harry
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here is the explanation step by step, I'm just taking the main method,

public static void main(String[] args){
Mammal h = new Horse(); // Horse class gets instantiated, since Horse is a Cattle, Cattle class gets instantiated, since Cattle is a Mammal, Mammal class gets instantiated.
Cattle c = new Horse(); // Same process happens here....try to explain it yourself.
c.eat(h);
}

Now in the c.eat(h) method, you are passing a Mammal reference. You have 3 eat methods with each method taking their class type as the reference. So the compiler will try to first look for the method in the Cattle class (since you are invoking c.eat(h)) where you have the eat method that takes Cattle. But you are passing Mammal reference which is too big for a method that takes Cattle reference (Imagine this situation as you are trying to invoke a method with long as argument that takes a int). Please note that this all happens at compile time itself.

Now, since the method is eat method inherited from Mammal superclass, it tries to invoke that inherited eat method that takes Mammal reference and it matches, so you get the o/p.

Did you understand this?
[ June 04, 2007: Message edited by: Jothi Shankar Kumar Sankararaj ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It is an example of method overloading.
You are passing reference of Mammal. Don't confuse with the run time type of
the Mammal that is Horse; Always see the reference type in such cases.
Cattle has inherited version of eat(Mammal) that is called when you pass Mammal ref variable to eat method using Cattle reference.


Thanks,
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jothi & CM Bhatt...its clear now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!