Subclasses and Method Privacy

Java specifies that methods may not be overridden to be more private.

Legal overridden method access


  Private -> Default -> Protected -> Public
  
  • A private method may be overridden by a private, default, protected, or public method.
  • A default method may be overridden by a default, protected, or public method.
  • A protected method may be overridden by a protected or public method.
  • A public method may be overridden only by a public method.

class BaseClass {
  protected void methodA() {
  }
}

class SubClass extends BaseClass{
  void methodA(){}
}

The output when compiling the code above:


Cannot reduce the visibility of the inherited method from BaseClass
Home 
  Java Book 
    Class  

Methods:
  1. Syntax for Method Creation
  2. Recursion
  3. Method with Parameters
  4. Pass-by-value vs Pass-by-reference
  5. What is Methods Overloading
  6. The main() Method
  7. Using Command-Line Arguments with main method
  8. Subclasses and Method Privacy