Win a copy of Spark in Action this week in the Open Source Projects forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

needs explanation

 
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?
 
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
 
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
 
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
 
No holds barred. And no bars holed. Except this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic