• Post Reply Bookmark Topic Watch Topic
  • New Topic

Refering to a Sub-Type of the Declared Type (Polymorphism) Question  RSS feed

 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to get a grasp on a reference variable of a particular type refering to a sub-type. Based on the following code:

Are the conclusions in the following comments correct? I have tried this so many different ways, and I think this is the conclusion I have come to. Am I correct?:

x.generalMethod(); //OK
x.specificMethod(); //won't compile
x.overrideMethod(); //compiles, but will call the Specific's (overridden) version only

Thank you in advance,
-Russ




 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.

The compiler has to decide which methods you can call, and the compiler only knows about the reference type (General, in this case). It doesn't try to work out what the actual type of the object is (and in some cases it would be impossible to). So you can only call methods in General, which explains the first two cases.

Precisely which version of the method is called is decided at runtime, and at runtime the actual object is known. This explains the third case, and is how polymorphism works.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
You could get the second one to compile by casting x to Specific
((Specific ) x).specificMethod();
but this risks a ClassCastException at runtime if x turns out not to refer to a Specific instance

Edit: bit slow today. Did it really take me more than 4 minutes to type that ?
 
Russ Russell
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Matthew and Joanne. I'm glad to know I came to the correct conclutions after wrestling this. Thanks for the validation.

-Russ
 
Keith Rainey
Ranch Hand
Posts: 66
Android Eclipse IDE IntelliJ IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, x.specificMethod(); is invalid because the superclass does not contain that method.


You can supply a downcast to use the superclass reference variable to execute the subclass's specificMethod() as ((Specific) x).specificMethod();


 
Campbell Ritchie
Marshal
Posts: 56595
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . but casting down on reference types is hazardous and error-prone, as Joanne took 4 minutes to tell you. Try something different. Try overriding generalMethod(). Try a specificMethod() like this in the superclassThen you don’t need any casts.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Joanne took 4 minutes to tell you

Actually it was only 3 minutes 22 seconds. Not only am I a slow typer, I'm not too good at maths either.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!