Member Access and Inheritance
A subclass cannot access the private members of the superclass.
For example, consider the following simple class hierarchy:
If you try to compile the following program, you will get the error message.
class A {
private int j; // private to A
}
class B extends A {
int total;
void sum() {
total = j; // ERROR, j is not accessible here
}
}
The output:
The field A.j is not visible
Home
Java Book
Class
Java Book
Class
Access Control:
- Access Control
- Member Access and Inheritance
- Class Member Access Protection and Package
- Modifiers and Features