Vishnu Vishnu - 5 months ago 7
Java Question

Why should we declare interface methods as public?

When I implement an

interface
method, I am forced to make it a
public
method.

We may have cases where we want to use either the
default
(like in case of access within the same package) or
protected
.

Can anyone please explain the reason behind this limitation?

Answer

Interfaces are meant to define the public API of a type - and only that, not its implementation. So any method (or static member) you define in an interface is by definition public.

Since an interface can't contain any concrete implementation, there is no way to call any member methods from within. And declaring such methods but leaving the calls to them to subclasses or totally unrelated clients would mean your type definition is incomplete and brittle. That is why if you need to define protected or package access members, you can do so in an abstract class (which may also contain implementation).