Showing posts with label Method Overriding. Show all posts
Showing posts with label Method Overriding. Show all posts

Monday, September 7, 2026

Java: Attempting to Assign Weaker Access Privileges Error

Access Specifiers in Method Overriding – Java

As per the rules of method overriding, you cannot use a weaker access specifier in the child class when overriding a method from the parent class.

For example, if the parent class has a method display() with a protected access specifier, the child class can override it using protected or public, but cannot use private.

Access Level

  1. Public

  2. Protected

  3. Default (package-private)

  4. Private

Access specifiers play an important role in inheritance and method overriding in Java.

Example:

/*
 * @Author TechBytes
 */

class AccessTest {

    protected void display() {
        System.out.println("Hello AccessTest:Display");
    }
}

class TestWithMain extends AccessTest {

    // Trying to override with a weaker access specifier
    // This will result in a compilation error.

    private void display() {
        System.out.println("Hello TestWithMain:display");
    }

    public static void main(String[] str) {

        AccessTest acObj = new TestWithMain();
        acObj.display();
    }
}

Error:

TestWithMain.java:8: display() in TestWithMain cannot override
display() in AccessTest;
attempting to assign weaker access privileges; was protected

private void display() {

The error occurs because the parent class method is protected, while the overriding method in the child class is private.

Correct Approach:

You can change the access specifier of the child method to protected or public.

Using public:

public void display() {
    System.out.println("Hello TestWithMain:display");
}

OR

Using protected:

protected void display() {
    System.out.println("Hello TestWithMain:display");
}

Both approaches work because the child class is not reducing the visibility of the parent method.

Note:

An overriding method can have the same or wider access than the method in the parent class, but it cannot reduce the access level.

Hope it helps you understand access specifiers and method overriding in Java.


Access Modifiers in Method Overriding: Rules and Examples in Java

As per the rule in overriding , you cannot apply weaker access specifier over a stronger access specifier.Suppose , your parent class has a method Display() with stronger access specifier like Public, then you cannot use weaker access specifier like private or public when you are overriding the Display() method.

Note :-


 Public is the 1st stronger access specifier
 Protected is the 2nd stronger access specifier
 Default is the 3rd stronger access specifier
 Private is the most weaker access specifier

 Always keep in mind about the access specifiers has vital role in inheritance (OOPS concept).

 Example :-


class AccessTest{
    protected void display(){ //Stronger Access specifier
        System.out.println("Hello AccessTest:Display");
    }
}

class TestWithMain extends AccessTest{
    // I am trying to override with weaker specifier , it show error
    // You can use public or protected (higher or same level of access specifier)
    private void display(){
        System.out.println("Hello TestWithMain:display");
    }   
    public static void main(String str[]){       
        AccessTest acObj=new TestWithMain();
        acObj.display();
    }   
}
 
 Error :-

 TestWithMain.java:8: display() in TestWithMain cannot override display() in AccessTest;
 attempting to assign weaker access privileges; was protected
    private void display(){

   

So, now if you change the access specifier to protected or public then it will work properly.

Changed executable code :-

   public void display(){
        System.out.println("Hello TestWithMain:display");
    }

   
    OR
   
    protected void display(){
        System.out.println("Hello TestWithMain:display");
    }

  Hope it will help you.