In Java, a default method is a method that has a default implementation and can be overridden by a class implementing the interface.
This feature was added to Java 8 interface to provide a way for interface evolution and to allow for the addition of new methods to an interface without breaking existing code.
Default methods are defined using the default keyword in the method signature and can be overridden in the implementing class using the override keyword.
Let’s understand default methods in Java interface with an example:
interface Shape { void draw(); default void resize() { System.out.println("Resizing shape"); } }
Here, the Shape
interface contains an abstract and a default method. We have another example that implements this interface and provides new implements of the default method.
class Circle implements Shape { @Override public void draw() { System.out.println("Drawing Circle"); } } class Square implements Shape { @Override public void draw() { System.out.println("Drawing Square"); } @Override public void resize() { System.out.println("Resizing Square"); } }
In the above example, the Shape
interface has a draw method and a resize method with a default implementation.
The Circle
and Square
classes implement the Shape
interface, and the Square
class overrides the resize method to provide its own implementation.
When we call the resize method on an object of the Circle
class, it will use the default implementation in the Shape interface:
class MainClass { public static void main(String[] args) { Circle c = new Circle(); c.resize(); } }
Output:
Resizing shape
And when we call the resize method on an object of the Square
class, it will use the overridden implementation:
class MainClass { public static void main(String[] args) { Square s = new Square(); s.resize(); } }
Output:
Resizing Square
This way, default methods provide a way to add new functionality to an interface without breaking existing code and allow implementing classes to choose whether to use the default implementation or provide their own.
In Java 8, the interface may have default methods along with the abstract method which creates a question, what is the difference between interface and abstract class? As the interface now can contain non-abstract methods as well as abstract classes.
Let’s have a look at some major points:
An abstract class and a Java 8 interface in Java serve different purposes and have different characteristics. Here are some key differences between them:
In summary, we can say, an abstract class can be used as a base class for inheritance and provides a common implementation for its subclasses, while an interface defines a contract for its implementing classes and supports multiple inheritance. Starting with Java 8, an interface can also provide a default implementation for its methods.
Java provides several built-in interfaces that contain default methods. Some of the most commonly used interfaces with default methods are:
forEach()
, getOrDefault()
, merge()
, etc.stream()
and parallerStream()
.forEach()
, and spliterator()
.reversed()
, and reverseOrder()
.These interfaces were updated with default methods in Java 8 to provide additional functionality and make it easier to add new methods to the interfaces without breaking existing code.