Java 9 addsprivatemethodsnew features ininterface. Private methods can be defined using theprivatemodifier. We can addprivateandprivatestaticmethodsin the interface of
Java 9Rules for private methods in interfaces:
interface{ private methodName(parameters) { // some statements } }
interface TestInterface { default void methodOne() { System.out.println("This is a Default method One..."); printValues(); // calling a private method } default void methodTwo() { System.out.println("This is a Default method Two..."); printValues(); // calling private method... } private void printValues() { // private method in an interface System.out.println("methodOne() called"); System.out.println("methodTwo() called"); } } public class PrivateMethodInterfaceTest implements TestInterface { public static void main(String[] args) { TestInterface instance = new PrivateMethodInterfaceTest(); instance.methodOne(); instance.methodTwo(); } }
This is a Default method One... methodOne() called methodTwo() called This is a Default method Two... methodOne() called methodTwo() called
The above is the detailed content of What are the rules for private methods in interfaces in Java 9?. For more information, please follow other related articles on the PHP Chinese website!