Evolution of Interface in Java
Last Updated :
25 Apr, 2025
In Java, interfaces define a contract for classes without specifying how the methods are implemented. With time, Java has introduced major enhancements or we can say evolution of interfaces to make them more powerful. So, in this article, we will explore the evolution of interfaces in Java from Java SE 7 to Java SE 21.
Interfaces in Java SE 7 and Earlier
In Java 7 or earlier, interfaces were very basic. They include only:
- Constant variables (implicitly public, static, and final)
- Abstract methods (implicitly public and abstract)
There was no way to provide method implementations inside interfaces.
Example:
Java
public interface Geeks {
String a = "Geeksforgeeks is the best.";
void hello(String a);
void world(int x);
}
Interfaces in Java SE 8 (Default and Static Methods)
After Java 7, Java 8 introduced a major change in interfaces. The change was the ability to add default and static methods in interfaces. This allowed developers to provide method implementations directly within interfaces.
Now, interfaces can contain:
- Constant variables
- Abstract methods
- Default methods
- Static methods
Example:
Java
public interface Geeks {
String b = "Sweta is an Employee.";
default void hello(String a) {
System.out.println("Hello");
}
static void world(int x) {
System.out.println("World");
}
// still abstract
void bye();
}
public interface Geeks {
String b = "Sweta is an Employee.";
default void hello(String a) {
System.out.println("Hello");
}
static void world(int x) {
System.out.println("World");
}
// still abstract
void bye();
}
Interfaces in Java SE 9 (Private Methods in Interface)
From Java 9 onwards, interfaces can contain private methods. These became helpful for code reuse within default and static methods inside the same interface.
Now interfaces can have:
- Constant variables
- Abstract methods
- Default methods
- Static methods
- Private methods
- Private static methods
Example:
Java
public interface Geeks {
String b = "Sweta is an Employee.";
default void hello(String a) {
// calling private method
greet();
}
static void world(int x) {
System.out.println("World");
}
void bye();
private void greet() {
System.out.println("Greetings from GFG.");
}
private static void log() {
System.out.println("Logging...");
}
}
Interfaces in Java SE 15 and Java SE 16 (Sealed Interfaces)
Java 15 introduced sealed interfaces as a preview feature. Then Java 16 made them official. Sealed interfaces restricts which classes can implement them using the permits keyword.
Example:
Java
public sealed interface Geeks permits GeeksImpl1, GeeksImpl2 {}
final class GeeksImpl1 implements Geeks {}
final class GeeksImpl2 implements Geeks {}
Interfaces in Java SE 21+ (Pattern Matching with Sealed Interfaces)
Java 21 has enhanced the sealed interfaces by supporting pattern matching in switch expressions. This simplifies complex type checks.
Example:
Java
sealed interface Shape permits Circle, Square {}
final class Circle implements Shape {
double radius;
}
final class Square implements Shape {
double side;
}
public class Main {
static void printArea(Shape shape) {
switch (shape) {
case Circle c -> System.out.println("Circle radius: " + c.radius);
case Square s -> System.out.println("Square side: " + s.side);
}
}
}
Tabular Representation:
Java Version | Interface Capabilities |
---|
Java 7 | Constant variables, Abstract methods |
Java 8 | + Default methods, Static methods |
Java 9 | + Private methods, Private static methods |
Java 16 | + Sealed interfaces |
Java 21+ | + Pattern Matching with sealed interfaces |
Similar Reads
Collection Interface in Java The Collection interface in Java is a core member of the Java Collections Framework located in the java.util package. It is one of the root interfaces of the Java Collection Hierarchy. The Collection interface is not directly implemented by any class. Instead, it is implemented indirectly through it
6 min read
Function Interface in Java The Function Interface is a part of the java.util.function package that has been introduced since Java 8, to implement functional programming in Java. It represents a function that takes in one argument and produces a result. Hence, this functional interface takes in 2 generics, namely as follows:T:
6 min read
Cloneable Interface in Java The Java.lang.Cloneable interface is a marker interface. It was introduced in JDK 1.0. There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object.clone() method valid thereby making field-for-field copy. This interface allows the implementing class to
4 min read
Java Functional Interfaces A functional interface in Java is an interface that contains only one abstract method. Functional interfaces can have multiple default or static methods, but only one abstract method. Runnable, ActionListener, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lam
7 min read
Interface Naming Conflicts in Java Interfaces in Java consist of abstract methods (which do not contain a body) and variables (which are public static final). Implementation of the methods of the interface is defined in classes that implement that interface. It helps Java to achieve abstraction. Naming Conflicts occur when a class i
5 min read
Static method in Interface in Java Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods ca
2 min read