Based on the core concepts of Java’s object-oriented design and the Liskov Substitution Principle (LSP), here is a review and markdown-formatted summary of the technical reasoning behind why subclasses cannot assign weaker access privileges.
java-subclass-access-privilege-rules.md
In Java, when you override a method in a subclass, the access modifier of that method must be the same as, or more accessible than, the method in the superclass. You cannot make it “weaker” (more restrictive).
The hierarchy of access from strongest (most accessible) to weakest (most restrictive) is:
publicprotecteddefault (no modifier)privateIf a superclass method is protected, the overriding method in the subclass must be protected or public. It cannot be default or private.
The primary reason for this restriction is the Liskov Substitution Principle. It states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
When a class defines a public or protected method, it is making a “contract” with the rest of the world that this method is available for use. If a subclass were allowed to make that method private, it would break that contract.
Consider this scenario:
class Super {
public void display() {
System.out.println("Super display");
}
}
class Sub extends Super {
// Imagine if this were allowed:
private void display() {
System.out.println("Sub display");
}
}
public class Main {
public static void main(String[] args) {
Super obj = new Sub(); // Upcasting
obj.display();
}
}
Super). It sees that display() is public, so it allows the call.display() in the actual object (Sub).Sub had marked the method private, the JVM would be forced to call a method it shouldn’t have access to, or throw a runtime error.By forcing subclasses to maintain or increase visibility, Java ensures that polymorphism remains type-safe. The compiler can guarantee that if a method is accessible on a parent reference, it will definitely be accessible on any child object assigned to that reference.
Java - Weaker Access Privilege is not allowed in Method Overriding - Practical Demo
This video provides a practical demonstration and code examples showing exactly what happens when you try to compile code that violates access privilege rules.
Here is the markdown-formatted review and summary of the concepts discussed at the source.
In Java, when you override a method in a subclass, the access modifier must be the same as or more accessible than the method in the superclass. You cannot make it “weaker” (more restrictive).
The hierarchy of access from strongest (most accessible) to weakest (most restrictive) is:
publicprotecteddefault (no modifier / package-private)privateExample: If a superclass method is protected, the overriding method in the subclass must be protected or public. It cannot be default or private.
The primary reason for this restriction is the Liskov Substitution Principle. It states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
When a class defines a public or protected method, it is making a “contract” with the rest of the world that this method is available for use. If a subclass were allowed to make that method private, it would break that contract.
Consider this scenario:
class Super {
public void display() {
System.out.println("Super display");
}
}
class Sub extends Super {
// Imagine if this were allowed:
private void display() {
System.out.println("Sub display");
}
}
public class Main {
public static void main(String[] args) {
// Upcasting: Sub is-a Super
Super obj = new Sub();
// At compile time, this is valid because Super.display() is public.
// At runtime, if Sub made it private, the JVM would hit an access violation.
obj.display();
}
}
Super). It sees that display() is public, so it allows the call.display() in the actual object (Sub).Sub had marked the method private, the JVM would be forced to call a method it shouldn’t have access to, or throw a runtime error. To prevent this “split personality” behavior, Java forbids it at the compiler level.By forcing subclasses to maintain or increase visibility, Java ensures that polymorphism remains type-safe. The compiler can guarantee that if a method is accessible on a parent reference, it will definitely be accessible on any child object assigned to that reference.
Source Reference: JavaThinking - Why can’t we assign weaker privilege in subclass?