This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

[enthuware] OO Concepts - Shadowing vs. overriding

 
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've just been through the OO concepts test on enthuware and this little gem of a question(which I must admit is a good one!) came up...



My initial thoughts was that it would print out "9 sportscar", because even though c is a "Car", the "sportscar" has both the gear ratio and accelerate, so I thought it would use both the variable and method from the subclass.

However, it appears that it will use the variable from the super class, and method from the subclass.

The explanation on the tool isn't that good, it mentions that variables are shadowed and not overridden...can anyone expand on that for me?

Cheers
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The binding of the variables is decided at compile time. So at compile time the compiler knows that c is of type Car and hence use the gerRatio from the Car. But in case of methods the actual version of the method invoked is done at runtime depending on whether the sub class has overridden the method.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Mohamed Sanaulla explained,

these are the things to remember-


  • Runtime polymorphism applies only to methods. Or in other words 'Dynamic method dispatch' is supported. (Finding the type of object at runtime and invoking the corresponding method)
  • In the case of variables, its always the 'reference type'. There's no such thing as 'Runtime Variable Dispatch'.. Everything is resolved at compile-time when it comes to variables


  • Now to shadowing:

    What happens, if you have a variable by the same name in the superclass and the subclass and what if they were initialized with different values ?

    Well, its all about the reference type with which the variable is accessed. If accessed with a reference of the superclass, you get the value you initialized with in the superclass
    If accessed with a reference of the subclass, you get the value you intialized with in the subclass. The actual type of the object doesn't matter (Whichever subclass object be assigned to the reference). Only the reference type matters.

    As to the word 'shadow'ing

    To say that gearRatio is shadowed in SportsCar, it means that the superclass's value is bypassed and the new value defined in the SportsCar is used. The word 'overriding' is avoided here as 'overriding' is a term that is tightly used along with 'runtime polymorphism'. And that isn't the case here

    -Vishwa
     
    Beware the other head of science - it bites! Nibble on this message:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic