When overriding a method, why can I increase access but not decrease it?

Why does Java specify that the access specifier for an overriding method can allow more, but not less, access than the overridden method? For example, a protected instance method in the superclass can be made public, but not private, in the subclass.


It's a fundamental principle in OOP: the child class is a fully-fledged instance of the parent class, and must therefore present at least the same interface as the parent class. Making protected/public things less visible would violate this idea; you could make child classes unusable as instances of the parent class.


Imagine these two classes:

public class Animal {
  public String getName() { return this.name; }
}

public class Lion extends Animal {
  private String getName() { return this.name; }
}

I could write this code:

Animal lion = new Lion();
System.out.println( lion.getName() );

And it would have to be valid, since on Animal the method getName() is public, even tho it was made private on Lion. So it is not possible to make things less visible on subclasses as once you have a superclass reference you would be able to access this stuff.


Take an example given below

 class Person{
 public void display(){
      //some operation
    }
 }

class Employee extends Person{
   private void display(){
       //some operation
   }
 }

Typical overriding happens in the following case

Person p=new Employee();

Here p is the object reference with type Person(super class) when we are calling p.display(). As the access modifier is more restrictive, the object reference p cannot access child object of type Employee