Search code examples
javainterfacesubclasssuperclass

doSomething() in ClassOne can't implement doSomething() in InterfaceOne, attempt to assign weaker access privilege, was public


SubclassOne extends ClassOne and implements InterfaceOne, both of which has a void doSomething(){} method. However, the compiler display error message,

doSomething() in ClassOne can't implement doSomething() in InterfaceOne, attempt to assign weaker access privilege, was public

Could someone please tell me why the compiler display this particular message? What is the reason behind it?

public class ClassOne {
    void doSomething(){
        System.out.println("do something from InterfaceMethod class");
    }
}


public interface InterfaceOne {
    default void doSomething(){
        System.out.println("do something from InterfaceOne");
    }
}


public class SubclassOne extends ClassOne implements InterfaceOne{
    public static void main(String[] args) {

    }
}

Solution

  • Methods in interfaces are public. Methods without access modifier package-private, i.e. weaker access privilege. Add public modifier to doSomething in ClassOne

    public class ClassOne {
        public void doSomething(){
            System.out.println("do something from InterfaceMethod class");
        }
    }
    

    You can see the access modifiers table at the documentation