*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes needs explanation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "needs explanation" Watch "needs explanation" New topic
Author

needs explanation

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
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

Joined: Apr 19, 2005
Posts: 39
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

Joined: Dec 28, 2004
Posts: 127
thanks
deepak.iam clear now
Ashok Kumar
Ranch Hand

Joined: Aug 27, 2004
Posts: 93
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.


"Decide what you want, decide what you are willing to exchange for it. Establish your priorities and go to work."
Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
thanks ashok
Jkarthi keyan
Greenhorn

Joined: Apr 27, 2005
Posts: 3
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: needs explanation
 
Similar Threads
Compile time polymorphism
knb book pgs 308-310 polymorphism con't.
my Overload program
Overloading-issue- Help me out guys - Imm.
Query on the topic overloading