• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

polymorphism doubt

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exactly
 
I want my playground back. Here, I'll give you this tiny ad for it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic