Take the 2-minute tour ×

When i implement an interface method, I am forced to make it a public method. We may have case where we want to use either the default(like in case of access within the same package) or protected. But why? Can anyone please explain the reason behind this limitation?

share|improve this question
If you want protected and private members or static methods and non-static fields, you can use an abstract class –  Peter Lawrey Mar 8 '12 at 9:28
add comment

3 Answers

up vote 32 down vote accepted

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

share|improve this answer
@peter-well that makes sense to some extent! –  vishnu Mar 8 '12 at 8:37
I think this answer is right, but it's basically a roundabout way of saying, "because that's what the Java folks wanted." You could come up with perfectly reasonable arguments for having protected methods, too (package-private might be a bit harder to justify). But you certainly can't have private methods, since those are never inherited. My guess is that, rather than saying "this subset of visibilities, and here's why this-but-not-that," they thought it'd be simpler to just say "here's the one visibility you get." –  yshavit Mar 8 '12 at 8:45
@yshavit, I tried to think about why the Java folks wanted things to be like this. One piece of information that was left out above is that they added interfaces to the language specifically because they wanted to disallow multiple inheritance, and all the problems it brought forth in C++. –  Péter Török Mar 8 '12 at 8:52
@yshavit, would be interested in any "perfectly reasonable arguments for having protected methods" though :-) –  Péter Török Mar 8 '12 at 8:53
+1 for illustrating the conceptual difference between interfaces and abstract classes. –  pap Mar 8 '12 at 8:56
show 6 more comments

Maybe this will provide some answers.

To my knowledge, you use interfaces to allow people from outside your code to interact with your code. To do this, you need to call your methods public.

If you would like to force someone to override a given set of private methods, you might want to declare an abstract class with a series of abstract protected methods.

share|improve this answer
"abstract private methods"... did you mean "abstract protected methods"? –  BoltClock Mar 8 '12 at 8:34
@npinti-well put in simplest terms! –  vishnu Mar 8 '12 at 8:40
@BoltClock: Yes you are correct. Fixed it thanks :) –  npinti Mar 8 '12 at 8:42
Or abstract default (package) scope methods –  Amir Pashazadeh Mar 8 '12 at 8:46
However, Joshua Bloch strongly encourages us to use interfaces as -types- and to use those types to refer to objects. It's a nice idea, but it develops two ways to conceptualize interfaces: as a mechanism for using user-defined types in a way that doesn't interfere will single-inheritance; and as an API contract. Since we have these two, I agree that it would be VERY nice if we did not have to make interface methods public in order to keep those UDT's that we did not want to export encapsulated. –  scottb Jun 9 '13 at 1:01
add comment

An interface is a contract that the class that implements it will have the methods in the interface. The interface is used to show the rest of the program that this class has the methods and that they could be called

share|improve this answer
Yes, but interfaces are also types. Sometimes, programmers want to use types that they've created without exporting them as part of the API. This is where forcing interface methods to be public is annoying. –  scottb Jun 9 '13 at 1:03
add comment

Your Answer


