Default Methods in Java 8 Interface | Code Examples Explained

Default Methods in Java 8 Interface | Code Examples Explained

What is Default Method in Java Interface?

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.

Abstract Class vs Interface in Java 8

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:

  • Inheritance:
    An abstract class can be a base class for inheritance and provide a common implementation for its subclasses, while an interface only defines a contract for its implementing classes and does not provide any implementation.
  • Multiple inheritance:
    An interface can be implemented by multiple classes, providing multiple inheritance, which is not possible with an abstract class.
  • Method implementation:
    An abstract class can have abstract and concrete methods, while an interface can only have abstract methods (methods without a body). However, starting with Java 8, an interface can have default methods with a default implementation.
  • Constructors:
    An abstract class can have constructors, while an interface cannot have any constructors.
  • Access Modifiers:
    Members of an abstract class can have access modifiers, such as public, private, and protected, to control their visibility and accessibility. In an interface, all methods are implicitly public, and the visibility cannot be changed.

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.

Built-in Default Methods in Java Interface

Java provides several built-in interfaces that contain default methods. Some of the most commonly used interfaces with default methods are:

  • java.util.List:
    This interface provides default methods for operations such as sorting the elements in a list.
  • java.util.Map:
    This interface provides default methods such as forEach(), getOrDefault(), merge(), etc.
  • java.util.Collection:
    This interface provides default methods such as stream() and parallerStream().
  • java.util.Iterable:
    This interface provides a default method such as forEach(), and spliterator().
  • java.util.Comparator:
    This interface provides default methods such as 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.

Leave a Reply

Your email address will not be published. Required fields are marked *