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 */ } |
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:
- Java interface Default methods helps us to extend interface without having a fear of breaking implementation classes.
- Java interface Default methods also referred as Defender Methods/ Virtual extension methods.
- One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
- 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 :
- Java interface Static() methods is a part of interface, but we canβt use it for implementation class objects.
- Static() allows us to move all static methods to interface level as anyways static() methods cannot be overridden.
- We cannot use static interface() method in object class method. We get compile time error.
- 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