31

Possible Duplicate:
Protected in Interfaces

In Java why cant I have protected methods in an interface?

Since according to Java specifications

protected access (denoted by the keyword protected) - a field or method accessible to any type in the same package, and to subclasses in any package.

If at all I have to use the interface, I am going to implement it and override the methods. So if I am going to implement where the class has access to those methods, since method accessible to in any package. So whats the harm in declaring the method as protected in Interface ?

2
  • 5
    Very good question. For almost every other thing in Java I have found a real reason for the choices made, but for this one I haven't. It makes perfect sense to me to define a protected method in an interface which allows another class inside the same package to use this method on an implementing object without requiring to expose that method, which may not be meant to be called by anyone other than the package members, to the rest of the world. Commented Oct 1, 2012 at 18:43
  • @MarkusA., +1, also see stackoverflow.com/questions/5376970/… Commented Aug 25, 2014 at 22:55

2 Answers 2

20

Protected methods are intended for sharing implementation with subclasses. Interfaces have nothing to offer as far as implementation sharing goes, because they have no implementation at all. Therefore all methods on interfaces must be public.

Sign up to request clarification or add additional context in comments.

13 Comments

Although I understand your point here, doesn't a public method in an interface force the implementing class to also share that method with the rest of the world? If an interface is being used to make the internals of an API more modular it doesn't mean that suddenly the developer wants to expose its internals to the rest of the world. With this limitation a method that has an implementation and is intended to be package-protected (default) is forced to become public because of the interface it is complying to. So although it makes sense for protected, it doesn't seem to for default
@jbx You are correct - package-private interfaces do create an interesting problem. However, I think it could be addressed at the compiler level by allowing package-private methods to provide implementations of package-private interfaces.
Actually I think that the interface itself can be package-private, so in actual fact what this restriction is doing is enforcing an all or nothing approach. You cannot have half the methods public and half of them package-protected. You either can see the interface with all its methods, or you can't. Maybe that's what the all-mighty creators intended after all :). It does pose the above problem however anyway. A public class implementing a package-protected interface will be forced to have the methods implementing the interface public.
@dasblinkenlight, You stated "interfaces have nothing to offer because they have no implementation". This is false, consider abstract classes, we have public abstract methods and protected abstract methods. The protected abstract methods are useful because implementors can override certain functionality while keeping that function away from public-access.
@Pacerier I talked specifically about Java interfaces, not about abstract classes. Of course abstract classes let you inherit implementation.
|
6

The interface of an object is the part of that object that is visible to external users of that class. On the contrary, protected and private methods (and fields) belong to the class internals. They are encapsulated inside the class and a class user should not be aware of them.

So, since interface is used to define interfaces (no pun intended), it is reasonable that they do not contain protected methods.

One doesn't want to think of implementation when defining an interface

1 Comment

I want. Because Java does not allow multiple inheritance, instead you have to use default and private methods in interface.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.