Interface In Java 8

Interface :

It is similar to a class in java, instead, with a keyword as interface. 
Interface used as method declaration, not for implementation untill jdk7

Eg:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
Public interface MyInteface {
 Void m1(String str);
 Void m2(int i);

 // But you cannot write as follows in interface
 /*Void m3(String s){
 System.out.println("You cannot write this way. "); 
     }*/

}




Interface in Java-8 :

Java-8 interface can have STATIC DEFAULT methods, we can also have method implementation from jdk 8 but has to be default

Eg: Before JAVA-8 

1
2
3
4
5
6
7
Public interface MyInterface{
     Void m1(String str);
     /* Default void m2(String str){
          System.out.println(β€œGives an error in prior jav-8 versions. ”);
    } //Gives you an error
  
  */
}



Code In Java-8


1
2
3
4
5
6
public interface MyInterfaceJava8 {
 Void m1(String str);
 Default void m2(String str) {
          System.out.println(β€œThis is a default implementation interface in Java 8. ”);
      }
}


Problem: Let’s say you have a class which inherits MyInterface and MyInterfaceJava8, both the interfaces have m2() method. When you override and call them, compiler does not know which method to call and throws you compile time error. In addition to that Java does not allow us to extend multiple classes because it will result in β€œDiamond Problem” as compiler does not know which superclass method to use.


Important point about Java Interface:

  1. Java interface Default methods helps us to extend interface without having a fear of breaking implementation classes.
  2. Java interface Default methods also referred as Defender Methods/ Virtual extension methods.
  3. One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
  4. Java 8 interface default methods will help us in avoiding utility classes, such as all the Collections class method can be provided in the interfaces itself.


Java Interface Static method

Java Interface static method is similar to default method, except for we can’t override them in implementation classes. This feature helps us in avoiding undesired results in case of poor implementation in implementation classes.

Eg: 

1
2
3
4
5
6
7
public interface MyInterface {
 Void m1(String str);
 
 static void m2(String str) {
          System.out.println(β€œThis is a static implementation interface in Java 8 ”);
      }
}


Now we have a class called MyClass


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public void MyClass implements MyInterface {
    Public static void main(String[] args){
  System.out.println(β€œThis is myclass implementing MyInterface”)
    MyClass obj= new MyClass();
  Obj.m2(β€œNihal Kurre”);
    }
    
 @Override //Give  you an error
 Void m2(String str){
  System.out.println(β€œstr”);
 }
}

Important point to remember :

  1. Java interface Static() methods is a part of interface, but we can’t use it for implementation class objects.
  2. Static() allows us to move all static methods to interface level as anyways static() methods cannot be overridden.
  3. We cannot use static interface() method in object class method. We get compile time error.
  4. Java static method provides good utility methods, check, collection sorting.


 Java Functional Interfaces :


If look into some other programming languages like c++, Javascript they are called functional programming languages bcoz, we can write functional and use whenever required.

 click here for More about functional Interfaces.



No comments:

Post a Comment