Reputation: 875
It is written in JLS (see section 8.3):
"A private field of a superclass might be accessible to a subclass - for example, if both classes are members of the same class. Nevertheless, a private field is never inherited by a subclass."
Could you give an axample of this statement?
I know that we can write:
public class MyClass {
private int x = 1;
public void testExample(MyClass m) {
m.x = 2;
}
}
Here we access private field m.x but we do not have "Super Class" - "Sub Class" here.
Upvotes: 3
Views: 1019
Reputation: 1500615
It's talking about nested classes - here's an example:
public class Test {
public static void main(String[] args) {
new Subclass(10).foo();
}
static class Superclass {
private int x;
Superclass(int x) {
this.x = x;
}
}
static class Subclass extends Superclass {
Subclass(int x) {
super(x);
}
public void foo() {
Superclass y = this;
System.out.println(y.x);
}
}
}
It's valid because of JLS 6.6:
Otherwise, the member or constructor is declared private, and access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor
Here the use of x
is within the body of Test
, which is the top level class enclosing the declaration of x
... although if you try to use x
unqualified, or just this.x
, that fails... precisely because x
isn't actually inherited (as per the piece of spec you quoted).
Upvotes: 3
Reputation: 20396
Method "visibility"-as the name implies-is about where methods and variables are "visible" to the programmer. As a general contract, variables scoped within a class are always visible within the class definition, even if they are declared private and being referred to by an instantiated object (not "this") of that class.
The rules regarding visibility and encapsulation are, design-wise, meant to assist with ensuring that programmers don't accidentally access variables and methods that would break functionality if used unexpectedly. For example, you'd break the contact of how java.util.Random worked if you were to manually call
private static long seedUniquifier() {
// L'Ecuyer, "Tables of Linear Congruential Generators of
// Different Sizes and Good Lattice Structure", 1999
for (;;) {
long current = seedUniquifier.get();
long next = current * 181783497276652981L;
if (seedUniquifier.compareAndSet(current, next))
return next;
}
}
(Sourced from the Sun JDK source code)
However, within the scope of code that you write, it's generally considered okay to call private variables/methods on objects defined as types of that class, since it is assumed that as the programmer and writer of the code in question, you have the authority, agency, and expertise necessary to manage the code correctly.
So in general, regardless of whether the variable is declared private or not, the following code:
public class Test {
private float internalValue;
public boolean isBigger(Test t) {
return internalValue > t.internalValue;
}
}
Will always be valid.
Upvotes: 0