I've read in a textbook that class members that are marked as private are not inherited.
So I thought if class A has a private variable x and class B would extend class A then there would be no variable x in class B.
However a following example shows that I misunderstood that:
public class testdrive
{
public static void main(String[] args) {
B b = new B();
b.setLength(32);
System.out.print(b.getLength());
}
}
class A {
private int length;
public void setLength(int len) {
length = len;
}
public int getLength() {
return length;
}
}
class B extends A {
public void dummy() {
}
}
The result is 32 and I'm confused because it looks like object with ref b now has the variable length and it's value is 32. But ref b refers to object created from class B where the length variable is not defined.
So what's the truth, does class B inherit the private variable length? If so, what does it mean that private variables are not inherited?