Default Methods with Interface in Java

Till Java 7

Till Java 7, Every method present inside interface is always public and abstract whether we are declaring or not. Because of this reason Interfaces were considered as 100% abstract.

All the variables present in the Interfaces are always public static final

public interface InterfaceMethods {
	// declared by user
	public abstract void addition();
	public static final int age = 28;
	
	// not declared by user
	void subtraction();
	String name = "Chercher Tech";
}
With Java 8:

Java 8 has brought methods two categories into Interfaces, which are :

  • default
  • static
With Java 9:

Java 9 started supporting private methods inside interfaces, Java 9 has supports features of previous releases like default and static methods.

In this article, we learn about default methods in java

The Need for Default methods in Interfaces

To add new methods to an Interface without affecting the classes that implement the Interface.

Please consider with respect to Java 7, When we write methods inside interface, We got to provide the implementation for those methods in the Class which is going to implement the Interface. If we do not provide an implementation for methods then we get below Compilation error.

The type Class must implement the inherited abstract method Interface.method().

For example, Consider an interface called MathsBrain which has two methods called add(), subtract().

Now all the classes which implement the Interface MathsBrain should provide an implementation for the methods present in the MathsBrain Interface. Now a few thousand classes are implemented using the MathsBrain

interface MathsBrain{
	public abstract void addition();
	public abstract void subtraction();
}
class Class01 implements MathsBrain{
	@Override
	public void addition() {
		// TODO Auto-generated method stub
	}

	@Override
	public void subtraction() {
		// TODO Auto-generated method stub
	}
}
class Class02 implements MathsBrain{
	@Override
	public void addition() {
		// TODO Auto-generated method stub
	}
	@Override
	public void subtraction() {
		// TODO Auto-generated method stub
	}
}

With the latest technology, developers of MathsBrain thinks to add a method called multiply(). Those thousand classes which already implemented the Interface MathsBrain must implement the new methods called multiple() otherwise compile-time error occurs when somebody uses those classes.

So, without affecting the child classes an Interface can not have a new method. This was the reason why the default method got Introduced in Interfaces of Java.

Calling default methods

We can call a default method in the classes which have implemented the Interface, default method only be accessed by creating an object to the Class which implements the interface.

default is a keyword so be aware that default is neither static not non-static, but a category just like static and non-static.

For example, Interface MathsBrain has a default method called multiple(), and Class A implements MathsBrain Interface, we have to create an object for Class A to call the multiply() method through the object.

A a = new A();
a.multiply();

The complete code for calling a default method present in Interface.

interface MathsBrain{
	default void multiply() {
		System.out.println("default method 'multiply' inside Interface");
	}
}
class A implements MathsBrain{
	public static void main(String[] args) {
		A a = new A();
		a.multiply();
	}
}

default-methods-in-java

Calling the default Method from another default Method:

We can call one default method from another default method present in the same interface without any object/reference.

Invoke the default method just with its name.

interface ICherCher{
	default void multiply() {
		System.out.println("default method 'multiply'");
	}
	default void addition() {
		multiply();
		System.out.println("default method 'addition'");
	}
}
class A implements ICherCher{
	public static void main(String[] args) {
		A a = new A();
		a.addition();
	}
}

calling-default-interface-methods-from-deafult-methods-java-interface

Calling the default Method from another Interface's default Method:

You cannot call default methods of one interface in Other Interface's default methods directly. This was not possible because default methods only can be accessed with Object. You cannot create Object to Interface in JAVA. Always there are workarounds for things.

To call a default method of one Interface in Another interface's default methods, we have to create a dummy class which implements the Interface, whose(Interface) default methods are going to get a call, this class does not require to contain any code.

In the below example, DummyB is a class which implements the interface ICherCher. default methods in the ITreat can access the default methods of ICherCher by creating an object to DummyB class.

interface ICherCher{
	default void multiply() {
		System.out.println("default method 'multiply'");
	}
	default void addition() {
		multiply();
		System.out.println("default method 'addition'");
	}
}
class DummyB implements ICherCher{
}
interface ITreat{
	default void division() {
		DummyB dumb = new DummyB();
		dumb.addition();
		System.out.println("default method 'division'");
	}
}
class A implements ITreat{
	public static void main(String[] args) {
		A it = new A();
		it.division();
	}
}

calling-default-interface-methods-from-deafult-methods-java

Override default method of Interface

Just like abstract methods, we can override the default methods present in the Interface. The child class has to provide the implementation to override the default method.

It is not mandatory to override the default methods

The syntax for overriding the default method.

public void defaultmethodName() {
	// own implementations
}

The complete example for overriding the default method present in the interface.

interface MathsBrain{
	default void multiply() {
		System.out.println("default method 'multiply' inside Interface");
	}
}
class A implements MathsBrain{
	public void multiply() {
		System.out.println("default method has been overriden");
	}
	public static void main(String[] args) {
		A a = new A();
		a.multiply();
	}
}

default-method-override-java-interface

In case, class X inherits MathsBrian and override default method multiply() and class A extends X. And if we create an Object to class A and call the default method multiply(), It will call the multiply() method from class X and not from Interface.

Default methods cause a 1/2 Diamond problem in Interface

You guys might be aware of what is a diamond problem if not let me provide two line explanation.

When a method is inherited from two different parent, and if the user calls that method then java will not be able to figure out from which parent to refer, this is called the diamond problem.

In case of interface, there is no Interface which is the parent to all interfaces, so instead of creating a diamond problem, Interfaces create 1/2 diamond or V problem. (I made the names up).

In Java, A class cannot inherit two classes at a time, i.e Multiple inheritance is not possible with classes. But a class can inherit two Interfaces, so Multiple inheritance is possible in Java in terms of Interfaces.

For example consider, Interface IA and IB, where both interfaces have a default method called multiply() . The class A Implements IA, IB.

half-diamond-java-interfaces-default-methods

package sample;

interface IA{lass A inherits unrelated defaults for multiply() from types IA 
	default void multiply() {
		System.out.println("default method 'multiply' inside Interface");
	}
}
interface IB{
	default void multiply() {
		System.out.println("default method 'multiply' inside Interface");
	}
}
public class A implements IA, IB{
	public static void main(String[] args) {
		A a = new A();
		a.multiply();
	}
}

If we execute the above program then we get below error.

java:13: error: class A inherits unrelated defaults for multiply() from types IA and IB
How do we solve this problem :

We can solve this problem by overriding the default method in class A, which implements both IA and IB interfaces.

While overriding we need to make a call to the default method using Interface name and super keyword

public void multiply() {
   // we need to explicitly mention the method with Interface name along with super keyword
	IB.super.multiply();
}

The complete code.

package sample;

interface IA{
	default void multiply() {
		System.out.println("default method IA");
	}
}
interface IB{
	default void multiply() {
		System.out.println("default method of IB");
	}
}
class A implements IA, IB{
	@Override
	public void multiply() {
		IB.super.multiply();
	}
	public static void main(String[] args) {
		A a = new A();
		a.multiply();
	}
}

default-method-multiple-occurances-interfaces-java

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions