Showing posts with label Interface Methods. Show all posts
Showing posts with label Interface Methods. Show all posts

Monday, September 7, 2026

Why Are Interface Methods Public in Java?

Why Are Interface Methods Public in Java?

Do you know why methods in a Java interface are public?

Consider the following example:

interface IMyInterface {

    public void display();

    public void calculate();
}

// Implementation class
class MyImplClass implements IMyInterface {

    @Override
    public void display() {
        System.out.println("Hi: Override-Display");
    }

    @Override
    public void calculate() {
        System.out.println("Hi: Override-calculate");
    }

    // Class's own method
    public void show() {
        System.out.println("Hi: I am Own Method");
    }

    public static void main(String[] args) {

        IMyInterface f = new MyImplClass();

        f.display();
    }
}

Output

Hi: Override-Display

Why must an interface method be public?

An interface defines a contract that implementing classes agree to provide.

An abstract interface method is implicitly public, so the following declarations are equivalent:

interface IMyInterface { void display(); public void calculate(); }

Both methods are public abstract methods.

When a class implements an interface, it must provide an implementation that is at least as accessible as the interface method.

For example, this is valid:

class MyImplClass implements IMyInterface { public void display() { System.out.println("Display"); } }

But this is not valid:

class MyImplClass implements IMyInterface { private void display() { System.out.println("Display"); } }

The compiler will report an error because a private method has weaker access than the public interface method.

The same applies to protected:

protected void display() { // Invalid }

You cannot reduce the visibility of an interface method when implementing it.

Why is this important?

Consider:

IMyInterface obj = new MyImplClass(); obj.display();

The reference type is the interface. The interface promises that display() is publicly available.

Therefore, the implementation cannot suddenly make that method private or protected.

The interface establishes the public contract, while the implementing class provides the actual implementation.

An important modern Java point

The statement "all methods in an interface are public" is no longer completely accurate.

Since Java 8, interfaces can also contain default and static methods. Since Java 9, interfaces can contain private methods.

For example:

interface IMyInterface { void display(); // public abstract default void print() { System.out.println("Default method"); } static void info() { System.out.println("Static method"); } private void helper() { System.out.println("Private helper"); } }

So the more accurate statement is:

Abstract methods declared in an interface are implicitly public. Interface default and static methods are also public by default, while interfaces can additionally contain private methods for internal implementation.

Key point

An interface defines a contract between the interface and its implementing classes. An implementing class cannot provide a weaker access level for an interface method.