Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the "Default" access specifier allowed for interfaces?

Tags:

java

interface

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?

like image 396
Anirudh Avatar asked Jun 04 '26 12:06

Anirudh


2 Answers

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.

like image 167
Andy Thomas Avatar answered Jun 06 '26 02:06

Andy Thomas


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.

like image 36
Ori Lentz Avatar answered Jun 06 '26 02:06

Ori Lentz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!