Why can't you reduce the visibility of a method in a Java subclass?

ria picture ria · Oct 21, 2009 · Viewed 12.2k times · Source

Why does the compiler give an error message when you reduce the visibility of a method while overriding it in the subclass?

Answer

Joachim Sauer picture Joachim Sauer · Oct 21, 2009

Because every instance of the subclass still needs to be a valid instance of the base class (see Liskov substitution principle).

If the subclass suddenly has lost one property of the base class (namely a public method for example) then it would no longer be a valid substitute for the base class.