Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

knb book pgs 308-310 polymorphism con't.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pg 308-309 it says that the following code

will print:

in the Animal version

.
but in the chart on page 310 the following code

will print:

Horse eating hay

.

My question is if both pieces are superclass references to a subclass object why is that the first one will call the superclass' method and the second calls the subclass's method?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you plz give few lines of code to make things easier to understand?
-PC
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look carefully...though both refer to the super class doStuff() is an overloaded function which is decided at the compile time based on the reference type, while eat() is overridden function and is decided at runtime based on the object type.
HTH
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code...

The lines you mentioned were:
Animal animalRefToHorse = new Horse();
ua.doStuff(animalRefToHorse);
As Sudhakar said, note how this is a reference of type ANIMAL even though it refers to a horse object. Since there are two methods...one taking a horse REFERENCE and one taking an animal REFERENCE, it uses the one with the ANIMAL reference...in fact, if that block were not present, the new line would fail to compile on ua.doStuff(animalRefToHorse);
This is because overloading (which is what this is) looks at the argument references. Once IN the method, a call to a method on ua would use polymorphism IF there were methods in Animal that were overrided in Horse.
Overloading uses the object references, and overriding uses the object subject to visibility constraints in the declared type (for example, if an Animal has no sleep method but Horse does and you call an animalReferenceToHorse.sleep, it would fail.
I am actually at the EXACT same point in the book as your question referred to (p. 313 to be exact).
Ross
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic