rohithtp-cv

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


Why Can’t We Assign Weaker Privileges in a Subclass?

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 Rule of Thumb

The hierarchy of access from strongest (most accessible) to weakest (most restrictive) is:

  1. public
  2. protected
  3. default (no modifier)
  4. private

If a superclass method is protected, the overriding method in the subclass must be protected or public. It cannot be default or private.


The Core Reason: Liskov Substitution Principle (LSP)

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.

The “Contract” Breakdown

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(); 
    }
}
  1. At Compile Time: The compiler looks at the reference type (Super). It sees that display() is public, so it allows the call.
  2. At Runtime: Due to polymorphism, the JVM attempts to call the version of display() in the actual object (Sub).
  3. The Conflict: If 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.

Conclusion

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.

more

Why Can’t We Assign Weaker Privileges in a Subclass?

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 Rule of Thumb

The hierarchy of access from strongest (most accessible) to weakest (most restrictive) is:

  1. public
  2. protected
  3. default (no modifier / package-private)
  4. private

Example: If a superclass method is protected, the overriding method in the subclass must be protected or public. It cannot be default or private.


The Core Reason: Liskov Substitution Principle (LSP)

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.

The “Contract” Breakdown

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(); 
    }
}
  1. At Compile Time: The compiler looks at the reference type (Super). It sees that display() is public, so it allows the call.
  2. At Runtime: Due to polymorphism, the JVM attempts to call the version of display() in the actual object (Sub).
  3. The Conflict: If 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.

Conclusion

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?