Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance and private variables

 
Eugene Kanshin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the detailed description.
Now I understand what is going on with this variable.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic