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  

Access Control:
  1. Access Control
  2. Member Access and Inheritance
  3. Class Member Access Protection and Package
  4. Modifiers and Features