Why a method must be public?

interface, java, oop

Solution

The answer is simple interface methods are always public or else just use composition instead of inheritance. Also to note that while overriding a method you can not narrow down the access level of the method.

The Oracle Docs says:

The access modifier public (§6.6) pertains to every kind of interface declaration.

Problem

Consider the following classes: ``` class A { void print() { System.out.println("A"); } } class B extends A implements C { } public interface C { void print(); } ``` I get this error: The inherited method A.print() cannot hide the public abstract method in C Now, I understand that `print()` must be public in order the eliminate the compilation error, but what's the reason behind this?

Original source

Related problems