Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Inheritance and private variables

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the detailed description.
Now I understand what is going on with this variable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic