CodesJava

Easy learning with example program codes

Java 8 Interface Changes default method and static method


Lambda expression is used to provide the implementation of functional interface. A Functional Interface is an interface with only single abstract method. In case of lambda expression, we don’t need to define the method again for providing the implementation so it saves lots of coding efforts.

Why default method?

For example we have an interface which has multiple methods, and multiple classes are implementing this interface. One of the method implementation can be common across the class. We can make that method as a default method, so that the implementation is common for all classes.

Second scenario where we have already existing application, for a new requirements we have to add a method to the existing interface. If we add new method then we need to implement it throughout the implementation classes. By using the Java 8 default method we can add a default implementation of that method which resolves the problem.

Java default methods

Java default methods are defined inside the interface and tagged with default keyword. Default methods are non-abstract methods. We can override default methods to provide more specific implementation.

Example

package com.codesjava;
 
interface Display{  
    //Default method   
    default void display(){  
        System.out.println("Hello Jai");  
    }  
    //Abstract method  
    void displayMore(String msg);  
}  
public class DefaultMethods implements Display{  
	//implementing abstract method
    public void displayMore(String msg){           
        System.out.println(msg);  
    }  
    public static void main(String[] args) {  
        DefaultMethods dm = new DefaultMethods(); 
        //calling default method 
        dm.display();    
        //calling abstract method
        dm.displayMore("Hello codesjava");    
    }  
}

Output

Hello Jai
Hello codesjava

Static methods in interfaces are similar to the default methods. The only difference is that we cannot override these methods in the classes that implements these interfaces.

Example

package com.codesjava;
 
interface Display{  
    //Default method   
    default void display(){  
        System.out.println("Hello Jai");  
    }  
    //Abstract method  
    void displayMore(String msg);  
    //Static method
    static void show(String msg){    
        System.out.println(msg);    
    }  
}  
public class StaticMethodTest implements Display{  
	//implementing abstract method
    public void displayMore(String msg){           
        System.out.println(msg);  
    }  
    public static void main(String[] args) {  
    	StaticMethodTest smt = new StaticMethodTest(); 
        //calling default method 
        smt.display();    
        //calling abstract method
        smt.displayMore("Hello codesjava");  
        //calling static method
        Display.show("Hello Java");
    }  
}

Output

Hello Jai
Hello codesjava
Hello Java
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki