Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private interface methods, example use-case?

"Support for private methods in interfaces was briefly in consideration for inclusion in Java SE 8 as part of the effort to add support for Lambda Expressions, but was withdrawn to enable better focus on higher priority tasks for Java SE 8. It is now proposed that support for private interface methods be undertaken thereby enabling non abstract methods of an interface to share code between them."

So says the specification for http://openjdk.java.net/jeps/213 and says in bug report https://bugs.openjdk.java.net/browse/JDK-8071453 .

But I can't really think of any use-case where this is necessary, even with the short explanation given above. May I ask for an example where "private interface methods" are useful in terms of code?

EDIT: So the answer is that due to how default implementations have been added to interfaces in Java 8, there can be instances where the default implementations are using the same codebase.

For example,

public interface MyInterface {      default void initializeMyClass(MyClass myClass, Params params) {          //do magical things in 100 lines of code to initialize myClass for example      }       default MyClass createMyClass(Params params) {          MyClass myClass = new MyClass();          initializeMyClass(myClass, params);          return myClass;      }       default MyClass createMyClass() {          MyClass myClass = new MyClass();          initializeMyClass(myClass, null);          return myClass;      } } 

Silly example, I know. But let's say that we want to use initializeMyClass(MyClass, Params) in both methods. However, if we do it like this (default method), then initializeMyClass(MyClass, Params) will be part of the public interface! To prevent that from happening, we can only keep the code of entire initializeMyClass(MyClass, Params) inside the createMyClass() default methods. Which results in code duplication, which is undesirable.

Therefore, this causes problem with refactoring, and to remove such code duplication, private default methods are allowed.

Thanks for answering!

like image 209
EpicPandaForce Avatar asked Mar 13 '15 19:03

EpicPandaForce


People also ask

What is the use of private methods in interface?

A private interface method is a special type of Java method that is accessible inside the declaring interface only. This means that no class that extends the interface can access this method directly using an instance of the class. Interface methods are public by default.

What is the use of interface explain with suitable example?

An interface is a description of the actions that an object can do... for example when you flip a light switch, the light goes on, you don't care how, just that it does. In Object Oriented Programming, an Interface is a description of all functions that an object must have in order to be an "X".

Can you have private methods in interfaces?

An interface can have private methods since Java 9 version. These methods are visible only inside the class/interface, so it's recommended to use private methods for confidential code. That's the reason behind the addition of private methods in interfaces.

What is the use of private methods in Java?

The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.


1 Answers

Interfaces can now have default methods. These were added so that new methods could be added to interfaces without breaking all classes that implement those changed interfaces.

If two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method and all its "implementation details" via the interface.

like image 146
mk. Avatar answered Sep 21 '22 21:09

mk.