Vishnu Vishnu - 1 year ago 53
Java Question

Why should we declare interface methods as public?

When I implement an

method, I am forced to make it a

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

Can anyone please explain the reason behind this limitation?

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download