JAVA 8-Interface Changes static and default method

Before Java 8 , interface in java can only have abstract methods. All the methods of interfaces are public & abstract by default. Java 8 allows the interfaces to have default and static methods. The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without affecting the classes that implements these interfaces.


Why default method?

For example, if several classes such as NOKIA, APPLE, SAMSUNG and OPPO implements an interface MOBILE Interface then if we add a new method to the MOBILE Interface, we have to change the code in all the classes(NOKIA, APPLE, SAMSUNG and OPPO) that implements this interface. In this example we have only four classes that implements the interface which we want to change but imagine if there are hundreds of classes implementing an interface then it would be almost impossible to change the code in all those classes. This is why in java 8, we have a new concept “default methods”. These methods can be added to any existing interface and we do not need to implement these methods in the implementation classes mandatorily, thus we can add these default methods to existing interfaces without breaking the code.

Static methods: in interfaces are similar to the default methods except that we cannot override these methods in the classes that implements these interfaces.


Example:Default method in Interface



public interface Mobile {

/*
* Already existing public and abstract method We must need to implement this
* method in implementation classes.
*/
void oldMethod(String string);

/*
* This is a default method so we need not to implement this method in the
* implementation classes
*/
default void newMethod() {
System.out.println("Newly added default method");
}

}


public class Nokia implements Mobile {

// implementing abstract method

@Override
public void oldMethod(String string) {
System.out.println("String is:" + string);

}


public static void main(String[] args) {

Nokia obj = new Nokia();

// calling the default method of interface

obj.newMethod();
// calling the abstract method of interface
obj.oldMethod("Implemented abstract method");

}


}

Output:

Newly added default method

String is:Implemented abstract method


                         Example:Static method in Interface





public interface Mobile {

/*
* This is a static method. Static method in interface is similar to default
* method except that we cannot override them in the implementation classes.
* Similar to default methods, we need to implement these methods in
* implementation classes so we can safely add them to the existing interfaces.
*/
static void anotherNewStaticMethod() {
System.out.println("Newly added static method");
}

/*
* Already existing public and abstract method We must need to implement this
* method in implementation classes.
*/
void oldMethod(String string);

/*
* This is a default method so we need not to implement this method in the
* implementation classes
*/
default void newMethod() {
System.out.println("Newly added default method");
}
}


public class Nokia implements Mobile {

// implementing abstract method
@Override
public void oldMethod(String string) {
System.out.println("String is:" + string);

}

public static void main(String[] args) {
Nokia obj = new Nokia();

// calling the default method of interface
obj.newMethod();
// calling the abstract method of interface
obj.oldMethod("Implemented abstarct method");
Mobile.anotherNewStaticMethod();

}
}

Output:


Newly added default method
String is:Implemented abstarct method
Newly added static method



This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments