3

As the interfaces in Java have methods and constants which are by default public, hence for me interface with default(or no) access specifier is somewhat contradictory as seen in the case below, though the methods in the interface are supposed to be public by default but the interface isn't even visible in packages outside the package it's defined.

package com.anirudh.package1;
interface IAccessSpecifierInterfaceTest {
    /**
     *
     * @param input
     */
    void implementMe(String input);
}

package com.anirudh.package2;

public class TryingToImplementDefaultInterfaceFromOtherPackage implements IAccessSpecifierInterfaceTest {
}

TryingToImplementDefaultInterfaceFromOtherPackage gives an error(can't resolve IAccessSpecifierTest

Does anyone know any practical scenario which warrant an interface with default access? and Why?

Anirudh
  • 2,286
  • 4
  • 38
  • 64

3 Answers3

3

An interface can be package-protected whenever it is part of the implementation of a package, rather than part of its public API.

Although the methods and fields are implicitly public, it is still valuable to prevent access to the interface as a whole.

For example, say you have a package that parses and executes a custom scripting language. The parsing is internal to the package. All the nodes in the abstract syntax tree implement a common interface. But that interface is not part of the public API of the package. It can be package-protected, to prevent it from being used outside of the package.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Doesn't explain why methods in an interface that can only be accessed within the package still need to be public. Basically, why not allow package private methods in package private interfaces? I am sure this could have easily been easily implemented in the compiler? I believe the OP knows what default access is, thus the question? – Chetan Kinger Apr 19 '15 at 15:30
  • That related question is addressed (poorly) in http://stackoverflow.com/questions/5376970/protected-in-interfaces . I suspect that during the early design of Java, it was considered that public access was the most frequent use case. Still, I've seen cases where I'd like to specify `protected` access to prevent access outside the package when implemented by public classes. – Andy Thomas Apr 19 '15 at 15:39
  • Even if public access was the most frequent use case, why enforce it as the only use case for methods? If the compiler can infer the access modifier while creating a default constructor for a class with no constructor, how bad would it be to do the same for interface methods? – Chetan Kinger Apr 19 '15 at 16:48
  • @bot - For this question, there's a good reason why interfaces can have default access. For your related question, I'm not sure we have a good answer. There is some speculation at http://stackoverflow.com/questions/9046012/why-interface-cannot-have-protected-methods . See in particular the first answer and its initial comments. – Andy Thomas Apr 19 '15 at 20:11
2

As with any other reason why to keep things "package protected".

Suppose for instance you have a method calling another method with a parameter specifying what should be done in several cases (most basic example: onSuccess and onError). You will most probably implement this in java using interfaces.

However, this is done for your own personal and internal use. You do not wish to expose this interface outwards. No one using your package has (or should have) access to the method in question, so there's no real reason why they should have access this interface.

A short code example explaining the scenario:

class DoingSomething {

    void doSomething(IResponse response) {
         if (/*do something*/) {
             response.onSuccess();
         } else {
             response.onError();
         }
    }

}

class AskForSomething {

      void ask() {
           new DoingSomething().doSomething(new IResponse() {

                void onSuccess() {
                     System.out.println("Success.");
                }

                void onError() {
                     System.err.println("error..");
                }
           });
      }
}

interface IResponse {
     void onSuccess();
     void onError();
}

In this case, DoingSomething and AskForSomething are package protected, and therefore IResponse should be package-protected as well, as no one outside the package can access either the interface or the classes / methods.

Ori Lentz
  • 3,668
  • 6
  • 22
  • 28
  • 1
    Why not allow package private methods in package private interfaces? I am sure this could have easily been easily implemented in the compiler? I believe the OP knows what default access is, thus the question? – Chetan Kinger Apr 19 '15 at 15:32
  • @bot what's the purpose to make methods "package scoped"? It's beyond of interfaces origin – Ernusc Apr 19 '15 at 16:31
  • @Ernusc `It's beyond of interfaces origin`. Explain? – Chetan Kinger Apr 19 '15 at 16:40
0

Interfaces with package scope do have their benefits but first you have to imagine, in which circumstances it does make sense to use package scope at all. The only thing I can think of are package specific implementations that may or should not be used outside the package. If you can live with that reasoning, you will certainly come to the conclusion that all design patterns apply even to this circumstances hence interfaces with package scope do make sense.

Now the only thing left to ask is, what are interfaces for? In short they define contracts in sense of what to expect from a class that implements that interface. Since a contract is public, i.e. you want to method and its signature to be usable, default can only be public.

Hannes
  • 2,018
  • 25
  • 32