In Java 9, interfaces can also have private methods. Apart from static and default methods in Java 8, this is another big change as it allows reusability of public # strong>Code is within the interface itself.
In the interface, it is possible to write common code on multiple default methods, thus generatingcode duplication. The introduction of private methods avoids this code duplication.
Advantages of private methods in interfaces<strong>interface interfacename { private methodName(parameters) { // statements } }</strong>
interface Test { default void m1() { common(); } default void m2() { common(); } private void common() { System.out.println("Tutorialspoint"); } } public class PrivateMethodTest implements Test { public static void main(String args[]) { Test test = new PrivateMethodTest(); test.m1(); test.m2(); } }
<strong>Tutorialspoint Tutorialspoint</strong>
The above is the detailed content of What are the advantages of private methods in interfaces in Java 9?. For more information, please follow other related articles on the PHP Chinese website!