Default methods in java 8

From Java 8 onward, it’s allowed to have methods in an interface with specifying the type as default. Prior to java 8 , it was only allowed to have abstract methods in interfaces. With this new addition in java8, now the interfaces can have their own implementation of methods without affecting the classes that implement the interface.

interface MyInterface {
 
  public void someOtherMethod();
 
  default void defaultMethod(){
     System.out.println("MyInterface default method");
  }
}
class MyImplementation implements MyInterface{
 
  @Override
  public void someOtherMethod() {
     System.out.println("My implementation");
  }
}
 
public class Main {
 
  public static void main(String args[]){
 
     MyImplementation implementation = new MyImplementation();
     implementation.someOtherMethod();
     implementation.defaultMethod();
  }
}

Output will be

My implementation
MyInterface default method

Why default methods were introduced ?

The original motivation to introduce default methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations.

For example, foreach method has been added to entire collection classes without breaking any of the existing implementations.

public interface Iterable<T> {
	public default void forEach(Consumer<? super T> consumer) {
		for (T t : this) {
			consumer.accept(t);
		}
	}
}

If this was done in the traditional way, it would break a lot of the existing implementation of the Iterable interface as it would require each of its implementations to override foreach method.

Default methods and multiple inheritance Ambiguity

By enabling the interfaces to have default methods, this will lead to multiple inheritances in java which was not supported earlier.

For example, two interfaces (MyInterfaceA  , MyInterfaceB) will have the same method defaultMethod. Another class (MyImplementation) is implementing these two methods. Either it has to provide its own implementation of the defaultMethod method or it has to specify from which interface method its using explicitly.

interface MyInterfaceA {
  default void defaultMethod(){
     System.out.println("Interface A default method");
  }
}

interface MyInterfaceB{
  default void defaultMethod(){
     System.out.println("Interface B default method");
  }
}

class MyImplementation implements MyInterfaceA,MyInterfaceB{

  @Override
  public void defaultMethod() {
     MyInterfaceA.super.defaultMethod();

     MyInterfaceB.super.defaultMethod();
  }
}

Output would be

Interface A default method
Interface B default method

Abstract classes vs default methods interfaces

With the addition of default methods to interfaces, it seems that interfaces and abstract classes are the same. But its NOT.! abstract classes can be still useful because of below reasons

  •  An abstract class can have the state, but you cannot have the state on the interface in Java.
  • Other restrictions such as having non-final variables, having constructors, etc. can be avoided using abstract classes.

 

 

Leave a Reply

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