Search code examples
javapolymorphismoverridingsubclassing

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


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


Solution

  • 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.