Design decisions: Why and when to make an interface private?
Asked Answered
L

4

17

Are private interfaces ever used in design decisions ? If so, what are the reasons and when do you know the need for a private interface?

Locomotive answered 1/1, 2011 at 9:55 Comment(2)
when the interface is about something that should be abstracted away from all the code that isn't in a specific package?Jackstay
An interface only can be private if it is a nested interface. A toplevel class or interface either can be public or package-private.Westfalen
O
10

A top-level interface cannot be private. It can only have public or package access. From the Java Language Specification, section 9.1.1: "Interface Modifiers":

The access modifiers protected and private pertain only to member interfaces whose declarations are directly enclosed by a class declaration (§8.5.1).

A nested interface can be private whenever it and its subclasses, if any, are an implementation detail of its top-level class.

For example, the nested interface CLibrary below is used as an implementation detail of the top-level class. It's used purely to define an API for JNA, communicated by the interface's Class.

public class ProcessController {
    private interface CLibrary extends Library {
        CLibrary INSTANCE = (CLibrary) Native.loadLibrary( "c", CLibrary.class );
        int getpid();
    }

    public static int getPid() {
        return CLibrary.INSTANCE.getpid();
    }
}

As another example, this private interface defines an API used by private nested classes implementing custom formatting symbols.

public class FooFormatter {
    private interface IFormatPart { 
        /** Formats a part of Foo, or text.
         * @param foo Non-null foo object, which may be used as input.
         */
        void write( Foo foo ) throws IOException;
    }

    private class FormatSymbol implements IFormatPart { ... }

    private class FormatText implements IFormatPart { ... }

    ...
 }
Oldie answered 28/10, 2015 at 18:42 Comment(5)
I like your second example.Mcdonough
@JordanStewart - Thanks. It's a very simplified example from some real-world code.Oldie
don't forget with java 8 you need those to declare complex functional interfaces without littering the namespaceEnthetic
@AndyThomas, What's the reason that FooFormatter implement IFormatPart?Oxalis
@Oxalis - It doesn't. IFormatPart is a nested interface inside `FooFormatter.Oldie
G
3

IMHO You cannot usefully make an interface private.

However I often have two interfaces, one for public use and one for internal use. The internal use interface I make package local if possible e.g.

public interface MyInterface {
   public void publicMethod();
}

interface DirectMyInterface extends MyInterface {
   public void internalUseOnlyMethod();
}

The internal use methods expose methods I don't want other developers to use and/or I want to be able to change easily. The reason I have the interface at all is that I have several implementations which I want to use internally via an interface.

Gowen answered 1/1, 2011 at 10:5 Comment(3)
For what purpose you need the internal-use-package? In more than 10 years of Java experience I never came about this pattern - but you say you use it often.Westfalen
I often write libraries or frameworks used by other developers. I want to keep the interfaces as simple as possible, so any methods I can hide away I do esp. if they are not intended to be used by other developers.Gowen
private interfaces are normal they are used for instance to declare function interfaces...Enthetic
T
1

It has to be package protected if the interface if for internal use. In general if the interface hasn't any interest outside it's ambit it's a good api design decision to hide it because there's less complexity for the users of the interface and also allows you to refactor it more easily, because when the interface is public and in the API you loss the liberty to change it.

Tourney answered 1/1, 2011 at 10:12 Comment(0)
A
0

A private interface method is a method that is only accessible within the class or object in which it is defined.

This allows for better organization and maintainability of code, as well as increased security by preventing external access to sensitive data or functionality.

Alate answered 30/1, 2023 at 8:52 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Simba

© 2022 - 2024 — McMap. All rights reserved.