jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes access to superclass's private instance variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access to superclass Watch "access to superclass New topic
Author

access to superclass's private instance variables

Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
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


OCPJP 85%
Ravindra Verma
Greenhorn

Joined: Sep 16, 2008
Posts: 8
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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]


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Joe Lemmer
Ranch Hand

Joined: Oct 24, 2008
Posts: 171
Thanks for your help Ravindra and Maneesh :-)

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38005
    
  22
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

Joined: Jul 26, 2007
Posts: 10170
    
    8

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

Joined: Oct 13, 2005
Posts: 38005
    
  22
Agree it is nothing personal. It shows there are two ways to do it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access to superclass's private instance variables
 
Similar Threads
Byte streams and the super keyword
package-private and protected access
Protected Access Modifier...
Using protected access modifier
what linking mechanism uses java (early or late)