Default methods in interface

Before understanding default methods in interface, first understand what in an interface. An interface is the contract an object exposes to the outside world. An interface is a reference type in Java. It is a collection of abstract methods which are implemented by class.

In plain terms, let’s say you are developing a program which interact with multiple TV sets. These TV sets will have different types of remote operating them. Now, if you implement version of our program for each type of TV and remote available in market, it will be a nightmare.
That’s why we expose an interface or contract which all TV set implementation adhere to and all remotes can use that interface to operate desired functionality.
Here advantage is that no matter what internal implementation of TV is as far as it follows the same interface, remote will work.

	interface TV { 
		public boolean switchOn();
		public boolean switchOff();
		public int changeChannel();
		public int volumeDown();
		public int volumeUp()
	} 

We can implement these methods in any which way we like on different TV sets (objects) without having to change remotes (outside world implementation).

public class SamsungTV implements TV {
	public boolean switchOn() {
		// implementation
	}

	public boolean switchOff() {
		// implementation
	}

	public int volumeDown() {
		// implementation
	}

	public int volumeUp() {
		// implementation
	}
}

As you noticed that interface only contains abstract function and does not give any implementation of any function. However, with Java 8 that has changed. An interface can have default implementation of a function.

	public interface InterfaceExample {
		// to be implemented by class
		void abstractMethod(String str);
	
		default void defaultMethod(String str){
			System.out.println("logging::"+str);
		}
	}

Why was that required? Imagine a case where you defined an interface and whole lot of classes implemented it in whatever way they wanted to. Now, at certain time in your project, you decided to add another method to the interface. It will need update of all classes which implement interface. 
How about a default implementation of new function and classes can implement it if they want different behavior? One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions.

As you know, Java does no allow multiple inheritance, that is to inherit more than one class, because that leads to “diamond problem. However, which interfaces having default implementation, and classes can implement multiple interfaces, “diamond problem” is bound happen. If two interface implement same function which function should be used by class object? 
To solve this problem, it is mandatory to provide implementation of common default methods of interfaces by class else compiler will throw errors.

	public interface InterfaceExample1 {
		// to be implemented by class
		void abstractMethod1(String str);
	
		default void defaultMethod(String str){
			System.out.println("logging::"+str);
		}
	}		
	public interface InterfaceExample2 {
		// to be implemented by class
		void abstractMethod2(String str);
	
		default void defaultMethod(String str){
			System.out.println("logging::"+str);
		}
	}		
public class ExampleClass implements InterfaceExample1, InterfaceExample2 {

	@Override
	public void abstractMethod1() {
	}

	@Override
	public void abstractMethod2(String str) {
	}

	@Override
	public void log(String str){
		System.out.println("MyClass logging::"+str);
	}
}

Default methods in interfaces actually bridges the gap between abstract classes and interfaces in Java. Also, it safeguards again breaking of code in case interfaces are modified. 

Please share if there is something wrong or missing. If you want to contribute to website, please reach out to us on communications@algorithmsandme.com