Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

needs explanation

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • 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?
 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks
deepak.iam clear now
 
Ashok Kumar
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks ashok
 
Jkarthi keyan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic