• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Help with static overriding behavior and hiding variables

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



OUTPUT


Hi. In MethodOverridingClass
Hi. In MethodOverridingClass
10
10
--------------
Hi. In ExtendedOverridingClass
Hi. In MethodOverridingClass
10
10
Hi. In ExtendedOverridingClass
Hi. In ExtendedOverridingClass
20
20



Hi,

As you can see above i am trying to figure out the behavior of inheritance wrt static methods and variables. I wanted to know

1- The behavior of inherited variables. As you can see the final variable in the superclass are overriden in the subclass
2- The behavior of static methods invoked in the case of an instance of a derived class. For example, am lost at why the program prints out "In MethodOverridingClass" while creating the second object.
 
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
You might want to read this OverridingVsHiding
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The link explains method overriding and hiding. Can you help out with variable hiding.

Also i get the the compiler looks at the declared type to invoke the class method. But it still doesn't explain what happened when i created the second object.
 
Sheriff
Posts: 22213
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And we can't tell you because you haven't given us the code for ExtendedOverridingClass.
 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 22213
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shridhar Raghavan wrote:1- The behavior of inherited variables. As you can see the final variable in the superclass are overriden in the subclass


No it is not. If you have read the article Mohamed linked to you would have seen that the final variable is hidden, not overridden. You cannot override variables, nested classes or static methods. Only instance methods can be overridden. All else is hidden (or shadowed) instead.

2- The behavior of static methods invoked in the case of an instance of a derived class. For example, am lost at why the program prints out "In MethodOverridingClass" while creating the second object.


Because with hidden methods and variables, the reference type determines which method is called, not the actual type. In constructors, the reference type is always that of the class itself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic