Java 8 Interface Changes – static method, default method
Java Interface Default Method
We will get compile error if we will not override m() method in class C below::
interface A {
public default void m() {
}
}
interface B {
public default void m() {
}
}
class C implements A, B {
@Override
public void m() {
// TODO Auto-generated method stub
B.super.m();
}
}
Important points about java interface default methods:
- It will help us in extending interfaces without having the fear of breaking implementation classes.
- It has bridge down the differences between interfaces and abstract classes.
- It will help us in avoiding utility and base implementation classes.
- One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.
- If any class in the hierarchy has a method with same signature, then default methods become irrelevant.
- A default method cannot override a method from
java.lang.Object
. It will give compile error. - Java interface default methods are also referred to as Defender Methods or Virtual extension methods.
Java Interface Static Method
Important points about java interface static method:
- Java interface static method is part of interface, we can’t use it for implementation class objects.
- Java interface static methods are good for providing utility methods, for example null check, collection sorting etc.
- Java interface static method helps us in providing security by not allowing implementation classes to override them.
- We can’t define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. This is because it’s not allowed in java, since Object is the base class for all the classes and we can’t have one class level static method and another instance method with same signature.
- We can use java interface static methods to remove utility classes such as Collections and move all of it’s static methods to the corresponding interface, that would be easy to find and use.
public interface MyData {
default void print(String str) {
if (!isNull(str))
System.out.println("MyData Print::" + str);
}
static boolean isNull(String str) {
System.out.println("Interface Null Check");
return str == null ? true : "".equals(str) ? true : false;
}
}
Now let’s see an implementation class that is having isNull() method with poor implementation.
public class MyDataImpl implements MyData {
public boolean isNull(String str) {
System.out.println("Impl Null Check");
return str == null ? true : false;
}
public static void main(String args[]){
MyDataImpl obj = new MyDataImpl();
obj.print("");
obj.isNull("abc");
}
}
Note that
isNull(String str)
is a simple class method, it’s not overriding the interface method. For example, if we will add @Override annotation to the isNull() method, it will result in compiler error.
Now when we will run the application, we get following output.
Interface Null Check
Impl Null Check
If we make the interface method from static to default, we will get following output.
Impl Null Check
MyData Print::
Impl Null Check
Comments
Post a Comment