Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

polymorphism doubt  RSS feed

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi,

On page 94, K&B, this line:


The methods you can call on a reference are totally dependent on the declared type of the variable, no matter what the actual object is, that the reference is referring to.



Then on page 102, we have this:

Polymorphic method invocations apply only to instance methods. You can always refer to an object with a more general reference variable type (a superclass or an interface), but at runtime the ONLY things that are dynamically selected based on the actual object (not reference type) are instance methods



These lines really confuse me all the time. i am unable to understand them when I read both of them side-by-side, they almost seem like contradicting each other (I know they are not, I am trying to understand why).

Please help!

- aditya
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below explains the 2 statements:
 
Aditya Kumar
Ranch Hand
Posts: 102
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think I understand it.

Would it be right to say that the reference type decides what method(s) can be invoked while the actual object type decides on which version of the "invokable" method to run?

 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aditya Kumar wrote:OK, I think I understand it.

Would it be right to say that the reference type decides what method(s) can be invoked while the actual object type decides on which version of the "invokable" method to run?


Yes.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!