Java

Java 8 – Interface Changes – Default Method And Static Method

Java interfaces prior to version 8 may only have abstract methods. By default, all interface methods are public & abstract. Interfaces may contain default and static methods according to Java 8. In order to enable developers to introduce new methods without altering the classes that implement these interfaces, interfaces provide default methods.

Why default method?

For instance, if several classes, such as A, B, C, and D, implement the interface XYZInterface, we will need to update the code in each of the classes (A, B, C, and D) that use this interface. In this example, there are only four classes that implement the interface that needs to be changed; however, if an interface were implemented by hundreds of classes, it would be nearly impossible to change the code in every class. Because of this, Java 8 introduces the idea of “default methods”. We can add these default methods to existing interfaces without breaking the code because they can be added to any existing interface and are not required to be implemented in the implementation classes.

Interfaces’ static methods are comparable to their default methods, with the exception that classes that implement such interfaces cannot override them.

Java 8 Example: Default method in Interface

MyInterface’s newMethod() function is a default method, therefore the implementation class Example doesn’t need to implement it. By doing so, we may extend the functionality of already-existing interfaces without worrying about the classes that implement them.

interface MyInterface{  
    /* 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");  
    }  
    /* Already existing public and abstract method
     * We must need to implement this method in 
     * implementation classes.
     */
    void existingMethod(String str);  
}  
public class Example implements MyInterface{ 
	// 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.newMethod();     
        //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

Since the static methods in interfaces are comparable to the default methods, as was already explained, we don’t need to implement them in the implementation classes. Without altering the code in the implementation classes, we may add them to the current interfaces without risk. These methods can’t be overridden in the implementation classes since they are static.

interface MyInterface{  
    /* 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");  
    }  
    
    /* 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 MyInterface{ 
	// 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.newMethod();     
        //calling the static method of interface
        MyInterface.anotherNewMethod();
        //calling the abstract method of interface
        obj.existingMethod("Java 8 is easy to learn"); 
        
  
    }  
}

Output:

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

Java 8 – Abstract classes vs interfaces

It seems that in Java 8, abstract classes and interfaces are equivalent due to the addition of default methods in interfaces. This is not fully accurate, however; just because we may now have concrete methods (methods with bodies) in interfaces, doesn’t imply that they are the same as abstract classes. There are still a few distinctions between them, one of which being that interfaces cannot have constructors but abstract classes are permitted to do so.

While abstract classes are meant to give some abstraction, interfaces are meant to provide complete abstraction. This is still accurate. With the inclusion of default methods, you can just state that we are able to add new features to the interfaces without impacting the end user classes. The interface functions as a blueprint for your class.

Default Method and Multiple Inheritance

When we have two interfaces with default methods that have the same signature, the multiple inheritance issue may arise. Take this as an example.

interface MyInterface{  
 
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void existingMethod(String str);  
}  
interface MyInterface2{  
	 
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void disp(String str);  
} 
public class Example implements MyInterface, MyInterface2{ 
	// implementing abstract methods
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public void disp(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.newMethod();     
  
  
    }  
}

Output:

Error: Duplicate default methods named newMethod with the parameters () and () are inherited from the types MyInterface2 and MyInterface

This is because the compiler is unsure which method to call because we have the same method in both the interface and the code.

How can this problem be fixed?
This method can be implemented in the implementation class in the following manner to address the issue:

interface MyInterface{  
 
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void existingMethod(String str);  
}  
interface MyInterface2{  
	 
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void disp(String str);  
} 
public class Example implements MyInterface, MyInterface2{ 
	// implementing abstract methods
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public void disp(String str){
    	System.out.println("String is: "+str); 
    }
    //Implementation of duplicate default method
    public void newMethod(){  
        System.out.println("Implementation of default method");  
    }  
    public static void main(String[] args) {  
    	Example obj = new Example();
    	
    	//calling the default method of interface
        obj.newMethod();     
  
  
    }  
}

Output:

Implementation of default method

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button