Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!