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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.