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