• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Overriding of final variables Vs final method

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, How can we override final variable. As expected, overriding a final method is giving error. Here the Code
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
overriding is only for methods and not variables. The FOO variable in your Sub hides the FOO in Super. try this and see what it is printing.
 
Saibabaa Pragada
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Super is the output. That means, instance/static variables do not participate in Polymorphism.

bhanu chowdary wrote:overriding is only for methods and not variables. The FOO variable in your Sub hides the FOO in Super. try this and see what it is printing.

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is correct. Static methods/variables only look at the reference type not the runtime type. Therefore is it's advisable to access static methods/variables with the classname and not with a reference type. Because otherwise when you change the referencetype you can invoke other methods.
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Saibabaa Pragada wrote:Super is the output. That means, instance/static variables do not participate in Polymorphism.



same is the case with static methods because eventhough you call a static metho with a reference variable, at runtime it will be classname.staticmethodname. please Write a sample code for this and check.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!