Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!