Sobes.tech
Middle

Is it possible to lower the access modifier level in Java?

sobes.tech AI

Answer from AI

In Java, you cannot lower the access level when overriding a method in a subclass. This means that if a method in the superclass is declared with a certain access level (for example, public), then in the subclass it cannot become protected or private.

The reason is that the subclass must be compatible with the superclass interface, and if a method was accessible from outside, its override should not restrict access.

Example:

class Parent {
    public void doSomething() {}
}

class Child extends Parent {
    // Compilation error: cannot narrow access
    // protected void doSomething() {}
}

Thus, the access level when overriding can only be widened or kept the same, but not reduced.

Is it possible to lower the access modifier level in… - sobes.tech