Interface Enhancements in Java 8



Before the release of Java version 8, the interface consists of only abstract methods and variables that defines the behavior that a class could implement. While adding a new method to an interface, it required change in all the implementing classes. It was not convenient for a large application. To overcome this, Java 8 introduced default and static methods in interfaces.

In Java, an Interface is a type of class that is defined using the keyword interface and has only method bodies without any implementations. To access its members within a class, we need to use the implements keyword while defining that class.

In this article, we are going to understand what enhancements were introduced for Interfaces when Java version 8 was released.

Changes in Interface from Java 8

Following are the changes introduced for Interfaces in Java 8:

  • Default Methods
  • Static Methods
  • Functional Interfaces with Lambda Expressions

Default Methods in Interfaces

A method defined using the default keyword inside an interface is called default method. Like other interface methods, it is also implicitly public which means a public specifier is not needed while defining. Unlike other interface methods, it can have implementation and hence, existing classes that implement the interface don't need to change unless they want to override the default method.

Example

The following example illustrates the use of the default method in an interface:

interface SpaceEntity {
   default void describe() {
      System.out.println("A space entity exists in this universe");
   }
}
class Planet implements SpaceEntity {
   @Override
   public void describe() {
      System.out.println("A planet orbits a star and may have moons");
   }
}
public class SolarSystem {
   public static void main(String[] args) {
      Planet earth = new Planet();
      // calls default method
      earth.describe();
   }
}

The result of the above code is as follows:

A planet orbits a star and may have moons

Static Methods in Interfaces

Static methods are defined using a non-access modifier called the static keyword. They can be used as utility methods without affecting the classes that implement the interface. This method can be called directly on the interface without requiring an instance.

Example

A Java program that shows the use of a static method in an interface is given below:

interface SpaceEntity {
   static void describe() {
      System.out.println("A space entity exists in this universe");
   }
}
class Planet implements SpaceEntity {
   public void describe() {
      System.out.println("A planet orbits a star and may have moon");
   }
}
public class SolarSystem {
   public static void main(String[] args) {
      // Calling the static method 
      SpaceEntity.describe(); 
      // Calling the overridden method 
      Planet earth = new Planet();
      earth.describe();
   }
}

The output of the above code is given below:

A space entity exists in this universe
A planet orbits a star and may have moon

Functional Interfaces with Lambda Expressions

In Java, a functional interface is an interface with only one abstract method but can have multiple default or static methods. From Java 8, we can use them with lambda expressions.

Example

In this example, let's see how a functional interface is used with a lambda expression:

@FunctionalInterface
interface SpaceEntity {
   void describe();
}

public class SolarSystem {
   public static void main(String[] args) {
      SpaceEntity planet = () -> System.out.println("A planet orbits a star and may have moons");
      // Calls the lambda expression 
      planet.describe(); 
   }
}

On running, you will get the following output:

A planet orbits a star and may have moons
Updated on: 2025-04-24T18:20:41+05:30

533 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements