Overriding a method can't ever reduce the visibility. Allowing that would violate the Liskov Substitution Principle, which states (simplified) that all objects of a derived class B
must have the same properties as the base class A
. In this case one such "property" would be a public method foo
which would be "lost" if B
had that same method, but made it protected
.
Also, since private
methods are not inherited (try calling it from a derived class!) they can't ever be overriden. You can have a public
method with the same name as a private
one in the base class, but that's not overriding, it's simply a new method with the same name, but not other relation. Calls to the private
method in the base class will not call the public
method in the superclass, even when executed on objects of the superclass!
In other words: private
methods never use runtime polymorphism.
See this sample:
public static class Base {
public void callBoth() {
foo();
bar();
}
private void foo() {
System.out.println("Base.foo");
}
protected void bar() {
System.out.println("Base.bar");
}
}
public static class Sub extends Base {
public void foo() {
System.out.println("Sub.foo");
}
public void bar() {
System.out.println("Sub.bar");
}
}
When executing new Sub().callBoth()
the output will be this:
Base.foo
Sub.bar