aspose file tools*
The moose likes Beginning Java and the fly likes Inheritance and private variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance and private variables" Watch "Inheritance and private variables" New topic
Author

Inheritance and private variables

Eugene Kanshin
Greenhorn

Joined: Oct 14, 2006
Posts: 7
I'm new to Java and my question might be stupid, but still...
Lets say I have a class with 1 instance variable and 2 methods (setter and getter):
class foo {
private int size;
public void setSize(int x){size = x;}
public int getSize(){return size;}
}
In order to incapsulate my variable I need to make it private and make my getter and setter methods public. But what would happen if I will make a subclass from my class - there won't be private variable in my subclass, but at the same time there will be public setter and getter methods for this variable. It sounds strange for me.


Я не злопямятный, я запишу...
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61610
    
  67

Your subclass will have the member, it just won't be directly accessible from the code of the subclass. The getter and setter, defined within the superclass, will still have access to the private member.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Nope, this isn't strange at all. This is just good encapsulation.

The variable itself remains private, and only the class that declares it ever has direct access to it. All access is mitigated through the setters and getters, which can have logic added or removed at any time.

In Java, it is best to encapsulate your variables and not expose them directly. After all, it is very rude to expose your privates.

-Cameron McKenzie
Eugene Kanshin
Greenhorn

Joined: Oct 14, 2006
Posts: 7
Thanks a lot, but it is still not very clear to me.
The private class members are not inherited in subclass (at least according to HedFirst Java).
So do I have an instance variable size in my subclass or not?
dinesh Venkatesan
Ranch Hand

Joined: Oct 12, 2006
Posts: 134
Hi Eugene,

You will not have the instance variable "size" in your subclass.
But obviously you will be having the inherited Accessor (getter) and Mutator(setter) in your subclass through inheritance.

So when you are trying to invoke either the accessor or the mutator it will call the superclass version of the corresponding method given that you have not overridden them.

Look at the following code:
class SuperClass {
private int onlyForMe;
public SuperClass() {
onlyForMe = 5;
}
public void setOnlyForMe(int ofm) {
onlyForMe = ofm;
}
public int getOnlyForMe() {
return onlyForMe;
}
}
public class SubClass extends SuperClass {
public static void main(String[] args) {
SubClass c = new SubClass();
System.out.println("WATCH OUT HERE "+c.getOnlyForMe());
}
}

Now you try to override the mutator something like this:

public void setOnlyForMe(int ofm) {
onlyForMe = ofm *2;
}

Then the compiler will throw an error
"onlyForMe has private access in SuperClass"

Hope this will help you!
dinesh.
Eugene Kanshin
Greenhorn

Joined: Oct 14, 2006
Posts: 7
Thanks for the detailed description.
Now I understand what is going on with this variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance and private variables