Showing posts with label Inheritance. Show all posts
Showing posts with label Inheritance. 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.

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.


Understanding the super Keyword and Constructors in Java

Understanding the super Keyword and Constructors in Java

When talking about inheritance in Java, it is important to understand that constructors are not inherited by a subclass.

A subclass can, however, invoke a constructor of its super class using the super keyword.

A few points about the super keyword

  1. super is a Java keyword.

  2. It can be used to access members of the super class, such as fields and methods.

  3. It can be used to invoke a super class constructor.

  4. When calling a super class constructor using super(...), it must be the first statement in the subclass constructor.

Example

class SUP {

    public SUP(String s) {
        System.out.println("Hi Super: " + s);
    }
}

// SUB class extending SUP class
class SUB extends SUP {

    public SUB(String p) {

        // Explicitly call the super class constructor
        // super(...) must be the first statement
        super(p);

        System.out.println("Hi SUB: " + p);
    }

    public static void main(String[] args) {
        new SUB("Rahul Sharma");
    }
}

Output

Hi Super: Rahul Sharma
Hi SUB: Rahul Sharma

When the SUB object is created, its constructor first invokes the SUP constructor using super(p). After the super class constructor completes, the remaining statements in the SUB constructor are executed.

What happens if super() is not written?

If the super class has an accessible no-argument constructor, Java automatically inserts a call to super() as the first statement of the subclass constructor, provided you don't explicitly call another super class constructor.

For example:

class SUP {

    public SUP() {
        System.out.println("Hi Super");
    }
}

class SUB extends SUP {

    public SUB() {
        // Compiler automatically inserts super();
        System.out.println("Hi SUB");
    }
}

The output is:

Hi Super
Hi SUB

However, if the super class does not have an accessible no-argument constructor, the subclass must explicitly invoke one of the available super class constructors.

Important point

Constructors are not inherited in Java. They are invoked as part of object construction.

The super keyword allows a subclass to explicitly invoke a super class constructor and access super class members.