Default Method in Java 8 | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

Before Java 8, interfaces could have only abstract methods. The implementation of these methods has to be provided in a separate class. So, if a new method is to be added in an interface, then its implementation code has to be provided in the class implementing the same interface. To overcome this issue, Java 8 has introduced the concept of default methods which allow the interfaces to have methods with implementation without affecting the classes that implement the interface.

The default methods were introduced to provide backward compatibility so that existing intefaces can use the lambda expressions without implementing the methods in the implementation class. Default methods are also known as defender methods or virtual extension methods.

package com.codeFactory;

/**
 * @author code.factory
 *
 */
interface TestInterface {

	/*abstract method*/
	public void add(int a, int b);
	
	/*default method*/
	default void show() {
		System.out.println("Code Factory!");
	}
}

public class TestClass implements TestInterface {

	@Override
	public void add(int a, int b) {
		System.out.println(a + b);
	}

	public static void main(String... args) {
		TestClass testClass = new TestClass();
		testClass.add(5, 10);
		
		testClass.show();
	}
}

Output :

15
Code Factory!

Static Method :

The interfaces also have static methods which is similar to static method of classes.

package com.codeFactory;

/**
 * @author code.factory
 *
 */
interface TestInterface {

	/*abstract method*/
	public void add(int a, int b);
	
	/*static method*/
	static void show() {
		System.out.println("Code Factory!");
	}
}

public class TestClass implements TestInterface {

	@Override
	public void add(int a, int b) {
		System.out.println(a + b);
	}

	public static void main(String... args) {
		TestClass testClass = new TestClass();
		testClass.add(5, 10);
		
		TestInterface.show();
	}
}

Output :

15
Code Factory!

Default Methods and Multiple Inheritance :

In case both the implemented interfaces contain deafult methods with same method signature, the implementing class should explicitly specify which default method is to be used or it should override the default method.

package com.codeFactory;

/**
 * @author code.factory
 *
 */
interface TestInterface1 {

	/*default method*/
	default void show() {
		System.out.println("Code Factory 1!");
	}
}

interface TestInterface2 {

	/*default method*/
	default void show() {
		System.out.println("Code Factory 2!");
	}
}

public class TestClass implements TestInterface1, TestInterface2 {

	public static void main(String... args) {
		TestClass t = new TestClass();
		t.show();
	}
	
	public void show() {
		TestInterface1.super.show();
		TestInterface2.super.show();
	}
}

Output :

Code Factory 1!
Code Factory 2!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s