• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Overriding and Overloading

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);



}

}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic