Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access to superclass's private instance variables

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • 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
 
Ravindra Verma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Maneesh Godbole
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for your help Ravindra and Maneesh :-)

 
Campbell Ritchie
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 11178
15
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 50217
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree it is nothing personal. It shows there are two ways to do it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic