15

Need to have more understanding about the private variables and inheritance. Earlier my understanding was if there is field in a class and when I'm inheriting the class, the fields that is not restricted by access(private variables) will be there in the inherited class. But I'm able use the private variables in base class if there is a public g/setter method.

How can I imagine a private variable in a base class.?

Kannan Ramamoorthy
  • 3,980
  • 9
  • 45
  • 63
  • 1
    I'm not entirely sure what the question exactly is. Of course you can use private fields with getters and setters, that's the purpose of getters and setters. – Kakalokia Mar 21 '13 at 14:08
  • I don't understand the question. An example demonstrating your problem would probably help. – jlordo Mar 21 '13 at 14:08
  • I have no idea what's your point. Please, try to explain it again. (and "variables" are called "fields" in java classes.) – Jean Waghetti Mar 21 '13 at 14:08
  • possible duplicate of [What does it mean for a method to be public/private/other in java?](http://stackoverflow.com/questions/2647289/what-does-it-mean-for-a-method-to-be-public-private-other-in-java) – BillRobertson42 Mar 21 '13 at 14:14

3 Answers3

18

This is what Java tutorial http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html says:

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.

Nevertheless, see this

class A {
   private int i;
}

class B extends A {
}

B b = new B();
Field f = A.class.getDeclaredField("i");
f.setAccessible(true);
int i = (int)f.get(b);

it works fine and returns value of field i from B instance. That is, B has i.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    Well sure, but all sorts of language guarantees go out the window when you use reflection. :) Private fields become public, final fields become mutable (for reference types), the world is your oyster. – yshavit Mar 21 '13 at 14:40
  • We might say that B does not inherit private fields of A, but the fact is that an instance of B has all fields of its all superclasses – Evgeniy Dorofeev Mar 21 '13 at 14:44
  • This is a hack which should be forbidden. You should really have a good reason to use reflection here. – Emad Van Ben Aug 11 '19 at 15:40
18
class A {
  private int a;
  public A(int a) { this.a = a; }
  public int getA() {return a;}
}

class B extends A {
  public B(int b) { super(b); }
  public int getB() {return getA();}
}

int result = new B(10).getA();

result will be 10. Private field a in class A is kind of inherited to B but B can't access it directly. Only by using the public/default/protected accessor methods defined in class A. B is A so it always has all the same fields that are in A and possible some new fields defined in class B.

oikku
  • 542
  • 2
  • 7
7

private variables / members are not inherited. That's the only answer.

Providing public accessor methods is the way encapsulation works. You make your data private and provide methods to get or set their values, so that the access can be controlled.

Sudhanshu Umalkar
  • 4,174
  • 1
  • 23
  • 33