• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question about shadowing

 
Nick Marino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain this to me, When I run this code the output is "8 Accelerate : SportsCar", but if gearRatio in the subclass is shadowing the gearRatio in the super class why would the gearRatio be output as 8 and not 9? I even ran the debugger on it and it shows that 8 is hidden yet it outputs 8.

I know the variables are shadowed and the methods are overridden, so I thought the output should be "9 Accelerate : SportsCar".

 
nemo zou
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another "pass by value and pass by reference" question. I believe there are numerous explanation about this question in the forum...
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run this code the output is "8 Accelerate : SportsCar"

Since accelerate() is overridden in class SportsCar, it is resolved at runtime i.e. the JVM sees that c is a SportsCar and c.accelerate() calls the accelerate() method in class SportsCar.

Since gearRatio is a field, it is not overridden. Since it is not overridden, it is resolved at compile-time (not runtime) i.e. the compiler sees that c is a Car and c.gearRatio refers to the gearRatio in class Car.

BTW, gearRatio is not shadowed in class SportsCar, it is hidden.
 
Nick Marino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I understand overriding the methods and being selected at run-time.

So basically because the reference variable is a Car and the actual object is SportsCar the method is chosen at run-time is the overridden method but yet the shadowed variables are based on the reference type.

What throws me off is that when I debug this I can clearly see that the gearRatio of the super class is hidden which would imply that the out put is going to see and use the shadowed variable.




 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Marino wrote:So basically because the reference variable is a Car and the actual object is SportsCar the method is chosen at run-time is the overridden method but yet the shadowed variables are based on the reference type.

This is correct (except that the variable is hidden, not shadowed). In others words, Car is the compile-time type of c and SportsCar is the runtime type of c.
 
Nick Marino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but what does hidden mean in the debug image I included, doesn't that mean the seen value is 9, yet the code outputs the super classes gearRatiao value.

Maybe I am miss reading that debug message, am I to assume that to the Sportscar instance its hidden so the only other value to return is 8 from the super class.

 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The debugger is not very useful here because the value "8" is resolved at compile-time, not runtime.

In your diagram, the debugger is giving you a runtime view of the SportsCar class but the value "8" is resolved in the Car class. At runtime, the SportsCar class has a gearRatio field [value = 9] which hides the gearRatio field [value = 8], therefore, the gearRatio field [value = 8] is hidden.

The gearRatio field [value = 8] would have been inherited from the Car class if it wasn't hidden by gearRatio field [value = 9].

According to the JLS
Hiding, in the technical sense defined in this specification, applies only to members which would otherwise be inherited but are not because of a declaration in a subclass.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nick Marino,

First of all, a warm welcome to CodeRanch!

Nick Marino wrote:What throws me off is that when I debug this I can clearly see that the gearRatio of the super class is hidden which would imply that the out put is going to see and use the shadowed variable.

Which IDE/tool are you using? Because I have never seen this representation (with reference of the hidden field and value). Apparently Eclipse also mentions both gearRatio variables, but it mentions nothing but the variable names

Nick Marino wrote:ok but what does hidden mean in the debug image I included, doesn't that mean the seen value is 9, yet the code outputs the super classes gearRatiao value.

Maybe I am miss reading that debug message, am I to assume that to the Sportscar instance its hidden so the only other value to return is 8 from the super class.

The debug message is pretty useless with this code snippet. It would be useful if you replace the accelerate method in SportsCar with this codeWith this code the debug message is very helpful: line1 will print the yellow gearRatio and line2 will print the hidden gearRatio.

But with the code snippet from the main method it's completely uselessThe reason why is already very well explained by Joe Bishara: the gearRatio is resolved at compile-time, not at runtime (and your debug message - like mine from Eclipse - is created at runtime).

Finally I share some very easy but very important rules:
1/ Which instance methods you can call/invoke is determined at compile time based on the reference variable type. Which instance method is actually executed is decided at runtime based on the type of the actual object (= polymorphism)
2/ Which instance variables you can access is determined at compile time based on the reference variable type. (no polymorphism with fields in Java)
3/ Which class variables and methods you can access is determined at compile time based on the reference variable type. (no polymorphism with class variables and methods in Java)

And don't forget: with class variables and methods you don't need an instance to access/invoke these methods. So what do you think the result of this code will be?

Hope it helps!
Kind regards,
Roel
 
Nick Marino
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome.

I am using BlueJ to write and debug just code snippets I am writing in preparation for the OCA exam.

Thanks for tips those are really clear to understand.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic