Default method in interface in Java 8

 214 total views,  2 views today

In this tutorial let us learn about Default method in interface in Java 8.

Java interfaces can now have default methods.Prior to Java 8, interfaces can only have public abstract methods.

It was not possible to implement a method in the interface itself and It was also not possible to add new functionality to the existing interface without forcing all implementing classes to create an implementation of the new methods

When we need to add any new functionality to interface,then all the implementation classes must undergo changes.

Starting with Java 8, interfaces can have static and default methods that, despite being declared in an interface, have a defined behavior.

Default method in interface

default keyword is used to declare a default method in an interface. These are accessible through the instance of the implementing class and can be overridden.

Let us create an Interface Vehicle and add a default method getOverview()

interface Vehicle {
	void start();

	void stop();

	default String getOverview() {
		return "Vehicle overview ";

Assume that this interface is implemented by the class VehicleImpl. For executing the default method an instance of this class should be created.

Vehicle vehicle = new VehicleImpl();
String overview = vehicle.getOverview();

Note: Implementation classes can override default methods.

If you would like to learn about static methods in an interface, please refer to static method in an interface in Java 8.

Reference : Official documentation

Thanks for reading Default method in interface.Please provide your valuable comments.

About the Author


Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us ( and give a thumbs up :) Feel free to contact me for any queries!.