Java8 - Learn Interface Default and Static Methods tutorials with examples

In this blog post, You are about to learn Default and static methods in interfaces tutorials with examples.

Why Default Methods interface is required?

Default Methods is one of the features introduced for the java 8 version.

Before Java 8 version, The interface has only abstract methods. Implementations of abstract methods have to be provided by the class which implements the interface.

If any interface code is released, We can’t change the interface structure.

if any changes are required for the interface, It causes code breaking in classes that implement interfaces.

Interface declaration contains abstract methods which have no implementation as described in the below example.

class ClassTwo implements MyInterface {
 public void methodone() {
 }
}
interface MyInterface {
 public void methodone();
}

In the above example, Declared a class by implementing MyInterface. This works perfectly.

Now add one more method - methodtwo in an interface like below.

interface MyInterface {
    public void methodone();
    public void methodtwo();

}

After adding new methods to the existing interface,ClassTwo gives a compilation error and breaks the functionality.

The type ClassTwo must implement the inherited abstract method MyInterface.methodtwo(). The fix for this is to provide an implementation for a new method.

This enforces clients to extend their functionality whenever the interface changes. This is a contradiction to class design.

ClassTwo compilation successfully when you implemented the new method.

To avoid this type of design problem, the Default method in Interface is introduced in java8.

Default methods added extra functionality to interface to without breaking the structure of classes that implement it.

Default methods are backward compatible with the previous version.

default keyword important notes

  • default is a keyword that works in interfaces from the java 8 version only
  • interface methods must prefix with keyword default
  • default methods in implemented classes are not required to provide implementation
  • Default method can be overridden
  • Default methods provide backward compatibility
  • These can also be called defend or virtual methods in java

Default Method in interface Example

In the below code, methodDefault() is default method in InterfaceDemo.

Added extra functionality to interfaces without breaking the structuring of implementation classes.

public class MyClass implements InterfaceDemo {
 @Override
 public void methodHeader() {
  System.out.println("methodHeader Implementation");
 }
 public static void main(String[] args) {
  MyClass myClassObject = new MyClass();

  // call interfaec abstract method
  myClassObject.methodDefault();
  // Call Interface abstract method
  myClassObject.methodHeader();
 }
}
interface InterfaceDemo {
 // This is default method
 default void methodDefault() {
  System.out.println("Default method");
 }
 // This is abstract method
 void methodHeader();
}

Output:

Default method
methodHeader Implementation

Interface Inheritance with Default Method example in java

As you know java doe not support multiple inheritances by extending classes and causes diamond issues, To achieve multiple inheritances, multiple interfaces are used.

With the default keyword in interfaces, Multiple inheritances cause the method ambiguity issue.

In the below code,

We defined two interfaces with the same default method names and created a class by implementing multiple interfaces as seen below.

interface InterfaceOne {
    default void method(){
       System.out.println("InterfaceOne.method()");
    }
}

interface InterfaceTwo {
    default void method(){
       System.out.println("InterfaceTwo.method()");
    }
}

public class ClassOne implements InterfaceOne, InterfaceTwo{
}

The code throws compilation error as lie Duplicate default methods named a method with the parameters () and () are inherited from the types InterfaceTwo and InterfaceOne.. Fix for duplicate default named method is to override default method as like below.

public class ClassOne implements InterfaceOne, InterfaceTwo{
    public void method(){
        System.out.println("class method");
     }
}

To call the default method of InterfaceOne, use the super keyword with the method name in the implemented method.

public void method(){
     InterfaceOne.super.method();

    }

Interface Static Method example

Static methods are similar to default methods in interfaces. They add extra functionality in the interface without breaking code in implementation classes. a static method cannot override in implementation classes.

public class MyClass implements InterfaeDemo {
 @Override
 public void methodHeader() {
  System.out.println("methodHeader Implementation");
 }
 public static void main(String[] args) {
  MyClass myClassObject = new MyClass();

  // call interfaec abstract method
  myClassObject.methodDefault();

  // Call Interface abstract method
  myClassObject.methodHeader();
  // interface static method is calling
  InterfaeDemo.methodStatic();
 }

}
interface InterfaeDemo {
 // This is default method
 default void methodDefault() {
  System.out.println("Default Method");
 }
 static void methodStatic() {
  System.out.println("Static Method");
 }
 // This is abstract method
 void methodHeader();
}

Output:

Default Method
methodHeader Implementation
Static Method

Difference between Abstract methods and default interface methods in java8?

The interface has now Default methods with the body. Abstract has concrete methods. But both are not the same.

An abstract class has a constructor, the interface doesn’t have a constructor.

Abstract classes define structured states with them. Interface added default implementation. Both are defined for a different purpose.

Difference between Default methods and static methods in an interface?

A static method cannot be overridden in implementation classes, Default method can be overridden in the implementation classes.

The static method can be accessed by Interface in the implementation class since these belong to Interfaces. Default methods can be accessed by objects of the implementation class. Class And interface contain same static method names, but they are not overriding each other.