Extension Method of Java

Hello Friends

Here is the example
  1. interface MyInterface {// Abstract Method  
  2. String enter(String name);  
  3. // Non-Abstract Method by using “default” Keyword  
  4. default String exit(String name) {  
  5. return “Hello ” + name +”, you are using exit method of MyInterface interface”;  
  6. }  
  7. }  
  8. public class MyClass{  
  9. public static void main(String[] args) {  
  10. // we are implementing MyInterface interface as an anonymous object  
  11. //and overridding abstract method  
  12. MyInterface f1 = new MyInterface() {  
  13. @Override  
  14. public String enter(String name) {  
  15. return “Hello ” + name +”, you are using enter method of MyInterface interface”;  
  16. }  
  17. };  
  18. // calling both abstract as well as non-abstract methods of interface  
  19. System.out.println(f1.enter(“Aman”));  
  20. System.out.println(f1.exit(“Aman”));  
  21. }  
  22. }  
Output 

Hello Aman, you are using enter method of MyInterface interface
Hello Aman, you are using exit method of MyInterface interface
Next Recommended Reading What is Web Container in Java?