• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

needs explanation

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal
{
}
class Horse extends Animal
{
}
public class UseAnimals
{
public void doStuff(Animal a)
{
System.out.println("In the Animal version");
}
public void doStuff(Horse h)
{
System.out.println("In the Horse version");
}
public static void main (String [] args)
{
UseAnimals ua = new UseAnimals();
Animal animalObj = new Animal();
Horse horseObj = new Horse();
ua.doStuff(animalObj);
ua.doStuff(horseObj);


Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);
}
}

OUTPUT:
In the Animal version
In the Horse version
In the Animal version

Hi All,
can anybody explain me how the output is generated for the above program?
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal
{
}
class Horse extends Animal
{
}
public class UseAnimals
{
public void doStuff(Animal a)
{
System.out.println("In the Animal version");
}
public void doStuff(Horse h)
{
System.out.println("In the Horse version");
}
public static void main (String [] args)
{
UseAnimals ua = new UseAnimals();
Animal animalObj = new Animal();
Horse horseObj = new Horse();
ua.doStuff(animalObj);
ua.doStuff(horseObj);


Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);
}
}

OUTPUT:
In the Animal version
In the Horse version
In the Animal version

Hi,
This is the Perfect Example of Overloading.. In Overloading the Compiler Check for the Reference type.


Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);

Here the Reference is of Animal. So the doStuff method that takes animal object as a Arguments gets executed...

Animal animalObj = new Animal();

Here the Reference is of Animal. So Animal version....

Horse horseObj = new Horse();
Here the Reference is of Horse. So Horse version gets executed...


Hope this will clarify your doubt...

Deepak
[ May 10, 2005: Message edited by: deepu Bhalotia ]
 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
deepak.iam clear now
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Venkat,

Nice to see a lot of questions from you.It covers most of the concepts.It seems like anyone preparing for SCJP has to just follow your threads and all his doubts will be cleared.
 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ashok
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also another point to be noted in this code i.e the base class reference is given to the subclass object.

Regards
Jkarthi
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic