Default method and static method

As name implies, default methods in java 8 are simply default. If you do not override them, they are the methods which will be invoked by caller classes. They are defined in interfaces.

Why we need default method?

For example, if several classes such as A, B, C and D implements an interface Interface then if we add a new method to the Interface, we have to change the code in all the classes(A, B, C and D) 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.

In short:

We can say that concept of default method is introduced in java 8 to add the new methods in the existing interfaces in such a way so that they are backward compatible. Backward compatibility is adding new features without breaking the old 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.

Java 8 Example: Default method in Interface

The method customMethod() in CustomInterface is a default method, which means we need not to implement this method in the implementation class Example. This way we can add the default methods to existing interfaces without bothering about the classes that implements these interfaces.

interface CustomInterface {
default void customMethod(){
System.out.println(“Newly added default method”);
}
void existingMethod(String str);
}

 

public class Example implements CustomInterface {
// implementing abstract method
public void existingMethod(String str){
System.out.println(“String is: “+str);
}
public static void main(String[] args) {
Example obj = new Example();
//calling the default method of interface
obj. customMethod();
//calling the abstract method of interface
obj.existingMethod(“Java 8 is easy to learn”);

}
}
Output:
Newly added default method
String is: Java 8 is easy to learn

Java 8 Example: Static method in Interface

As mentioned above, the static methods in interface are similar to default method so we need not to implement them in the implementation classes. We can safely add them to the existing interfaces without changing the code in the implementation classes. Since these methods are static, we cannot override them in the implementation classes.

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

/* 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 anotherNewMethod(){
System.out.println(“Newly added static method”);
}
/* Already existing public and abstract method
* We must need to implement this method in
* implementation classes.
*/
void existingMethod(String str);
}
public class Example implements CustomInterface {
// implementing abstract method
public void existingMethod(String str){
System.out.println(“String is: “+str);
}
public static void main(String[] args) {
Example obj = new Example();

//calling the default method of interface
obj. customMethod();
//calling the static method of interface
CustomInterface.anotherNewMethod();
//calling the abstract method of interface
obj.existingMethod(“Java 8 is easy to learn”);

}
}
Newly added default method
Newly added static method
String is: Java 8 is easy to learn

%d bloggers like this: