• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

State of variables in inheritance

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that variables are inherited in child class from base class.But how the variables behave when we declare same variables in child class as in base class.Are the variables in base class are overwritten or hidden?
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always declare private instance variables and use getter and setter methods to access them then this question will not arise.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables are hidden. For all practical purposes you can consider that they will never be inherited.

Check this link -> HiddenVars
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vvus bharadwaj wrote:I know that variables are inherited in child class from base class.But how the variables behave when we declare same variables in child class as in base class.Are the variables in base class are overwritten or hidden?



Variables are not overwritten. there is nothing overwritten , whether in case of methods(which can be overridden) or variables. when you declare variable with the same name as in superclass in your subclass , the variables are just hidden. they are not inherited, not overwritten. they are just not there in subclass/childclass. however you can use super reference to access the hidden variables.
reply
    Bookmark Topic Watch Topic
  • New Topic