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 ?

Community
  • 1
  • 1
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • 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. – Markus A. Oct 01 '12 at 18:43
  • @MarkusA., +1, also see http://stackoverflow.com/questions/5376970/protected-in-interfaces#comment-39795200 – Pacerier Aug 25 '14 at 22:55

2 Answers2

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 50
    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 Jan 17 '13 at 19:24
  • 1
    @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. – Sergey Kalinichenko Jan 17 '13 at 19:39
  • 1
    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`. – jbx Jan 17 '13 at 19:49
  • 3
    @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 Aug 25 '14 at 23:00
  • @Pacerier I talked specifically about Java interfaces, not about abstract classes. Of course abstract classes let you inherit implementation. – Sergey Kalinichenko Aug 25 '14 at 23:06
  • @dasblinkenlight, We are both talking about interfaces here. An abstract class defines an *interface*. In Java yes, the language limits us e.g. interface methods must be public, or class can only extend one parent class, but that's merely because of a Java limitation. Tell me then, what's the difference between an abstract class and an interface? – Pacerier Aug 25 '14 at 23:28
  • @Pacerier "what's the difference between an abstract class and an interface?" If you have a new question, click [Ask Question] button to ask it. – Sergey Kalinichenko Aug 25 '14 at 23:48
  • @dasblinkenlight, That's a rhetoric question; as such, it's to be interpreted as a statement. There is no difference between an abstract class and an interface, which is why you will not be able to answer the question "what's the difference between an abstract class and an interface?" simply because the answer is "there isn't". Also read the comment by Markus on http://stackoverflow.com/questions/5376970/protected-in-interfaces/5377004#comment-17111702 – Pacerier Aug 26 '14 at 00:11
  • @Pacerier That is so not true. In Java, an abstract class is a vehicle for sharing implementation, which is rather distinct from sharing an interface. This question has been asked before, [here is a link to lots of good answers](http://stackoverflow.com/q/18777989/335858). – Sergey Kalinichenko Aug 26 '14 at 00:17
  • @dasblinkenlight, Let me repeat the question: "what's the difference between an abstract class and an interface?" I'm not talking about *Java* abstract class vs *Java* interface, I'm talking about abstract class vs interface. And there's no difference between them. If you don't believe, **try saying one, just one difference**. And you will find yourself **unable** to do so, because there is none. (Oh, and btw Java 8 interfaces also allows method implementation, yet let allow me to repeat once again, I'm talking about abstract class vs interface, Not Java). – Pacerier Aug 26 '14 at 13:38
  • @Pacerier "I'm not talking about Java abstract class vs Java interface" Then there's no point in talking about it here, in a question clearly tagged `[java]`. – Sergey Kalinichenko Aug 26 '14 at 13:44
  • Even if we assume that this was a good enough answer at the time (I'm not convinced), this reasoning ceased to apply since Java 1.8 and the introduction of default implementations for interface methods. I've just hit a case where it would be nice to have package-private methods in an interface. – Fr Jeremy Krieg Jun 28 '19 at 07:23
  • public methods are also intended for sharing implementation, but there is no implementation in interface. – Alex78191 Sep 18 '19 at 13:49
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

Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
  • 2
    I want. Because Java does not allow multiple inheritance, instead you have to use default and private methods in interface. – Alex78191 Sep 18 '19 at 13:52