File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and Overloading

 
vimal kumar
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic