aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding and Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding and Overloading" Watch "Overriding and Overloading" New topic
Author

Overriding and Overloading

vimal kumar
Greenhorn

Joined: Aug 16, 2006
Posts: 6
How the call to a.call(a) works? Is it a combination of overloading and overriding

class Animal
{

public void call(Animal o)
{
System.out.println("SuperAnimal");

}
public void call(Horse o)
{
System.out.println("SuperHorse");

}


}

class Horse extends Animal
{

public void call(Horse o)
{
System.out.println("Horse");
}
public static void main(String... arg)
{
Animal a = new Horse();
Horse h = new Horse();

a.call(a);
a.call(h);



}

}
Mayank Chaudhary
Greenhorn

Joined: Mar 22, 2007
Posts: 1
Hi,

I think the output of this program will be:
SuperAnimal
Horse

Yes its a combination of both overloading & overriding
Overloading because you are overloading same method "call()" one with Animal parameter & other with Horse parameter.
Overriding because you have overridden the method call(Horse o) in subclass Horse

when you say :
a.call(a) --> Since a is of type Animal, it looks into Animal class & finds the matching method which takes Animal as parameter & executes & prints "SuperAnimal"(overloading)

a.call(h) --> here again it finds the matching method in Animal which takes Horse as parameter (till here overloading), but since the matched method is already overridden in the subclass, so at runtime JVM invokes the true object's (Horse) version of the method rather than the reference type's (Animal)
version. So the overriden method in the subclass executes & hence printing "Horse".

Hope this helps.
gadarla
Greenhorn

Joined: Mar 05, 2007
Posts: 3
Hi

O/p==
SuperAnimal
Horse

in my opinion
in super class those are method overloading.
and sub class method is overridden method to superclass horse parameter method
This is method overriding
vimal kumar
Greenhorn

Joined: Aug 16, 2006
Posts: 6
Whether the argument in a.call(a) refers the animal argument or horse argument since the base class reference variable actually referring to the sub class object
gianni ipez
Ranch Hand

Joined: Jan 02, 2007
Posts: 65
This is just a tricky question, useful to test overriding and overloading, but it's a very bad example of object oriented programming. Am I right?
Thanks
Gaurav Pavan Kumar Jain
Ranch Hand

Joined: Mar 19, 2007
Posts: 168
Hi VImal

It is a tricky combination of overloading and overriding.

the output will be "SuperAnimal", "Horse".

I am agree with Mayank
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
Hi Mayank,

when you say :
a.call(a) --> Since a is of type Animal, it looks into Animal class & finds the matching method which takes Animal as parameter & executes & prints "SuperAnimal" (overloading)

a.call(h) --> here again it finds the matching method in Animal which takes Horse as parameter (till here overloading), but since the matched method is already overridden in the subclass, so at runtime JVM invokes the true object's (Horse) version of the method rather than the reference type's (Animal)


I think your statement over here is a bit misleading. Actually, call to methods is determined at runtime & hence when the JVM finds that the reference "a" is pointing to a Horse object, it makes a call to the "call()" function in the Horse object. It never looks at the "call()" function in class A.

After this, it checks the method argument of "call()" function & accordingly calls the appropriate function which in the 1st case is :
which had been inherited from Animal but not overriden.

In the second case, it calls for the overriden "call()" function present in the Horse object & thus prints "Horse" & not "SuperHorse".

Only the call to the instance variables is resolved at compile time & not the methods. for ex cosider this code :


This code would print 10 because the compiler has decided at compile time itself that the value of Horse has to be taken from class Animal.
The same is not true for methods.


Regards,<br />Sourin.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding and Overloading