Home >Java >javaTutorial >What are the rules for private methods in interfaces in Java 9?
Java 9 adds privatemethodsnew features in interface. Private methods can be defined using the private modifier. We can add private and privatestaticmethods in the interface of
Java 9Rules for private methods in interfaces:
<strong>interface <interface-name> { private methodName(parameters) { // some statements } }</strong>
interface TestInterface { <strong>default </strong>void methodOne() { System.out.println("This is a Default method One..."); printValues(); // calling a private method } <strong>default </strong>void methodTwo() { System.out.println("This is a Default method Two..."); printValues(); // calling private method... } <strong>private </strong>void <strong>printValues</strong><strong>()</strong> { <strong>// private method in an interface </strong> 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(); } }
<strong>This is a Default method One... methodOne() called methodTwo() called This is a Default method Two... methodOne() called methodTwo() called</strong>
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!