Flat Preloader Icon

Java Default Methods

In Java, default methods were introduced in Java 8 to enable the addition of new methods to interfaces without breaking the classes that implement those interfaces. Prior to Java 8, interfaces could only declare abstract methods, meaning that any class implementing that interface would have to provide an implementation for all the interface’s methods.

Here's a Simple Example To Illustrate The Concept Of Default Methods In Java

				
					interface MyInterface {
    // Abstract method
    void existingMethod();

    // Default method
    default void newDefaultMethod() {
        System.out.println("This is a default method.");
    }
}

// Implementing the interface
class MyClass implements MyInterface {
    @Override
    public void existingMethod() {
        System.out.println
        ("Existing method implementation.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.existingMethod();
        // Output: Existing method implementation.
        obj.newDefaultMethod(); 
        // Output: This is a default method.
    }
}

				
			

Static Methods Inside Java 8 Interface

  • You can also define static methods inside the interface. Static methods are used to define utility methods. The following example explain, how to implement static method in interface?
				
					interface Sayable{    
    // default method    
    default void say(){    
        System.out.println
        ("Hello, this is default method");    
    }    
    // Abstract method    
    void sayMore(String msg);    
    // static method    
    static void sayLouder(String msg){    
        System.out.println(msg);    
    }    
}    
public class DefaultMethods implements Sayable{    
    public void sayMore(String msg){
    // implementing abstract method    
        System.out.println(msg);    
    }    
    public static void main(String[] args) {    
        DefaultMethods dm = new DefaultMethods();    
        dm.say();                       
        // calling default method    
        dm.sayMore("Work is worship");      
        // calling abstract method    
        Sayable.sayLouder("Helloooo..."); 
        // calling static method    
    }    
}    
				
			

Output:

				
					Hello there
Work is worship
Helloooo...
				
			

Abstract Class vs Java 8 Interface

  • After having default and static methods inside the interface, we think about the need of abstract class in Java. An interface and an abstract class is almost similar except that you can create constructor in the abstract class whereas you can’t do this in interface.
				
					abstract class AbstractClass{    
    public AbstractClass() {        // constructor    
        System.out.println
        ("You can create constructor in abstract class");    
    }    
    abstract int add(int a, int b);
    // abstract method    
    int sub(int a, int b){      
    // non-abstract method    
        return a-b;    
    }    
    static int multiply(int a, int b){
    // static method    
        return a*b;    
    }    
}    
public class AbstractTest extends AbstractClass{    
    public int add(int a, int b){        
    // implementing abstract method    
        return a+b;    
    }    
    public static void main(String[] args) {    
        AbstractTest a = new AbstractTest();    
        int result1 = a.add(20, 10);   
        // calling abstract method    
        int result2 = a.sub(20, 10);   
        // calling non-abstract method    
        int result3 = AbstractClass.multiply(20, 10); 
        // calling static method    
        System.out.println("Addition: "+result1);    
        System.out.println("Substraction: "+result2);    
        System.out.println("Multiplication: "+result3);    
    }    
}    
				
			

Output:

				
					You can create constructor in abstract class
Addition: 30
Substraction: 10
Multiplication: 200