Why does Java not allow private members in an interface?

Why doesn't Java allow private members in an interface? Is there any special reason?

+61
java
Apr 16 '12 at 6:35
source share
13 answers

From the Java Language Spec, (Access Control) :

"The Java programming language provides access control mechanisms to prevent users from using a package or class depending on unnecessary implementation details of that package or class."

Access control is the hiding of implementation details. The interface has no implementation to hide.

+70
Apr 16 2018-12-12T00:
source share

In Java 9, private methods in interfaces are possible.

Java 9 Features

The javac compiler team is pleased to announce the availability of a compiler supporting private methods in interfaces starting with the 9B54 JDK build.

+29
Mar 22 '15 at 14:07
source share

Private interface methods will differ from Java 9 as part of JEP-213 . Because interfaces in Java 8 can have default methods , private methods can allow multiple methods to use a common private method by default.

+12
Mar 16 '15 at 17:07
source share

An interface is used to describe the API that is provided by any class that implements the interface. Since the interface from its definition has no state, it makes no sense to declare field members in it.

+9
Apr 16 2018-12-12T00:
source share

It is not possible to implement such an interface. The answer to the question I posed strongly suggests that it would be impossible (without a radical change in the rules) to implement an interface with private methods - this leaves open the question of why protected and private private methods are not allowed.

class OuterClass { void run ( MyInterface x ) { x . publicMethod ( ) ; // why not? x . protectedMethod ( ) ; // why not? x . packagePrivateMethod ( ) ; // why not? x . privateMethod ( ) ; // why not? } interface MyInterface { public abstract void publicMethod ( ) ; // OK protected abstract void protectedMethod ( ) ; // why not? abstract void packagePrivateMethod ( ) ; // in interface default is public, but why not package private private void privateMethod ( ) ; // impossible to implement } class MyImpl implements MyInterface { public void publicMethod ( ) { } // ok protected void protectedMethod ( ) { } // no sweat void packagePrivateMethod ( ) { } // no sweat private void privateMethod ( ) { } // not happening } } 

The code below should achieve the desired result. Although all methods are publicly available, the public method is public. protected method effectively protected. packagePrivateMethod is effectively packagePrivate. privateMethod is virtually private.

 class WorkAround { void run ( MyPrivateInterface x ) { x . publicMethod ( ) ; x . protectedMethod ( ) ; x . packagePrivateMethod ( ) ; x . privateMethod ( ) ; } public interface MyPublicInterface { void publicMethod ( ) ; } protected interface MyProtectedInterface extends MyPublicInterface { void protectedMethod ( ) ; } interface MyPackagePrivateInterface extends MyProtectedInterface { void packagePrivateMethod ( ) ; } private interface MyPrivateInterface extends MyPackagePrivateInterface { void privateMethod ( ) ; } } 
+6
Apr 16 2018-12-12T00:
source share

According to Java scope of the programming language private members limited to the class in which it is declared and can only be accessed using the methods of this class . But inteface does not have a method body, so there is no use of declaring private members inside an interface .

+6
Apr 16 2018-12-12T00:
source share

As with Java 8, interfaces can have default methods, and with Java 9 an interface will be allowed to have private methods, to which only default methods in a single interface can be accessed.

+6
Jun 18 '17 at 8:23
source share

This is because they would be useless.

Unable to call private method.

Private members are implementation details. The interface is dedicated to the public role that the class can take.

+3
Apr 16 2018-12-12T00:
source share

private fields will not be completely useless, as other fields and inner classes can access them.

However, private methods cannot be implemented even in nested classes, which makes them almost useless. You can read them using reflection, but this is more of a marginal case.

+2
Apr 16 '12 at 7:32
source share

Java allows you to use private methods in the Java 9 interface . default methods were introduced in Java 8. It is possible that several methods by default want to share some code, then this code can be transferred to a private method without exposing it to the outside world. This bug was fixed and started with JDK 9 build 54, the compiler support for private interface methods was resurrected.

 public interface IData{ default void processData(int data) { validate(data); // do some work with it } default void consumeData(int data) { validate(data); // do some work with it } private void validate(int data) { // validate data } } 
+2
Apr 10 '17 at 9:10
source share

Private members do not make sense in the interface. An interface is a way to access a class with specific methods where you do not need to see inside that class.

Private members disagree with this.

+1
Apr 16 2018-12-12T00:
source share

Members of a class declared private do not inherit subclasses of this class. Only class members declared protected or public are inherited by subclasses declared in the package other than the one in which the class is declared.

Source

Thus, you do not have any working methods in the interface that can work with this private non-rotatable field. Then why should it exist?

0
May 7 '16 at
source share

Yes, I can’t do it. For all those who comment on why this should not:

Imagine I have a class A that uses interface I. Class B extends class A, so it also inherits all the interface methods in A.

Now imagine that I want a private method in class A, but I want it to be defined by contract for other classes (perhaps class C, which does not necessarily extend class B or A).

Perhaps for the “initialization” method, which I want for all classes using the I interface. But, obviously, I do not want the initialization method to be publicly available ... because it should be used only once or, as the class considers, it is not necessary just because you want to use it willy-nilly.

The only solution is a workaround or just forcing the init method to be used in the classes themselves without an interface.

I understand the reason is not too, of course, but still, it can come in handy sometimes. Obviously, Oracle agrees that they allow private interface methods in JDK 9.

What I did, for me, in any case, contained a simple logical variable, so the interface method (which should be private) can be marked as true (initialized = true) after installation once. Then, when called again, the method simply does nothing. Thus, the interface method can be implemented as public, but since the constructor (of my class) first calls this method, it sets the variable to true and therefore it cannot be called again.

Otherwise, you have to try another workaround if you want the inner workings of this class to use it ... perhaps the method itself sets the on and off flag when it uses it. When the flag is false, the method does nothing (this would be when someone called it from outside the class). However, when the classes associated with its methods call it, they quickly set the flag to true, then call the method, and then set the flag to false ??

After all, dumb. Probably now it’s just better to just put the private class in the class itself and completely cut the interface.

0
Nov 10 '16 at 0:36
source share



All Articles