• 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

access to superclass's private instance variables

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

I have made a superclass and several subclasses which extend it.

The superclass has private instance variables that I assumed the subclasses would all inherit without any issues, but when I try to compile my subclasses, I get the following error:



They are both in the same package and I assumed I was doing the right thing by making all the superclass instance variables private and have getter and setter methods which would also be inherited by the subclasses. Is this not right? Does anybody know where I am going wrong?

Many thanks

Joe
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Lemmer wrote:Hi there,

I have made a superclass and several subclasses which extend it.

The superclass has private instance variables that I assumed the subclasses would all inherit without any issues, but when I try to compile my subclasses, I get the following error:



They are both in the same package and I assumed I was doing the right thing by making all the superclass instance variables private and have getter and setter methods which would also be inherited by the subclasses. Is this not right? Does anybody know where I am going wrong?

Many thanks

Joe


Hey Joe,

I guess private members can not be accessed by sub classes or any other classes. They are visible to same class only. Thing is like you will never share your private things with anyone even your children.


You need to follow a fix pattern.

make class variables private
provide public getters/setters method

Hope it helps.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the instance variable protected instead of private and your subclasses can access it.

Recommended reading:
http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

[Edit]Wrote constructor instead of instance variable.[/Edit]
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Ravindra and Maneesh :-)

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make all the methods which access the variables in the superclass. Then you can use the superclass' public API and there is no need to gain access to the variables directly.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe its a personal thing, but I don't really agree to Campbell on this one.

The OP has defined sub classing and inheritance in the original scenario. Considering this, accessing the variable as would be more intuitive. In Campbell's code scenario, the getName() would be more helpful for external classes.

But like I said this is a personal consideration and in now way does it indicate anything wrong with Campbell's code.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree it is nothing personal. It shows there are two ways to do it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic