title: Private interface methods, example use-case?
body: <p>"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."</p>
  
  <p>So says the specification for  <a href="http://openjdk.java.net/jeps/213" rel="noreferrer">http://openjdk.java.net/jeps/213</a> and says in bug report <a href="https://bugs.openjdk.java.net/browse/JDK-8071453" rel="noreferrer">https://bugs.openjdk.java.net/browse/JDK-8071453</a> .</p>
  
  <p>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?</p>
  
  <p><strong>EDIT:</strong> 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.</p>
  
  <p>For example,</p>
  
  <pre><code>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;
       }
  }
  </code></pre>
  
  <p>Silly example, I know. But let's say that we want to use <code>initializeMyClass(MyClass, Params)</code> in both methods. However, if we do it like this (default method), then <code>initializeMyClass(MyClass, Params)</code> will be part of the public interface! To prevent that from happening, we can only keep the code of entire <code>initializeMyClass(MyClass, Params)</code> inside the <code>createMyClass()</code> default methods. Which results in code duplication, which is undesirable.</p>
  
  <p>Therefore, this causes problem with refactoring, and to remove such code duplication, private default methods are allowed.</p>
  
  <p>Thanks for answering!</p>