InterviewSolution
Saved Bookmarks
| 1. |
Private Interface Methods in Java 9 |
|
Answer» Java 9 introduced private methods and private STATIC methods. An interface can now have six features. The features are as follows:
The private methods increase reusability of code within interfaces. Their scope is limited to the interface itself and cannot be CALLED or accessed outside the interface. An example to show private methods in interfaces is as follows: public interface MethodInterface { public abstract void m1(); public default void M2() { m4(); //private method inside default method m5(); //static method inside other non-static method System.out.println("This is a default method"); } public static void m3() { System.out.println("This is a static method"); } private void m4() { System.out.println("This is a private method"); } private static void m5() { System.out.println("This is a private static method"); } } public class Example implements MethodInterface { @Override public void m1() { System.out.println("This is an abstract method"); } public static void main(String[] args) { MethodInterface OBJ = NEW Example(); obj.m1(); obj.m2(); MethodInterface.m3(); }The output is as follows: This is an abstract method This is a private method This is a private static method This is a default method This is a static method |
|