Pages

Wednesday, December 25, 2019

What is the need for Default Methods inside Interfaces in Java 8

1. Overview


In this java 8 tutorial series, We'll be learning today why default methods are introduced in java 8 and why default methods are needed?


need for Default Methods inside Interfaces in Java 8

2. Interfaces Before Java 8 


Prior to Java 8, Every method present inside interface is always public and abstract whether we are declaring it or not.

package com.java.w3schools.blog.java.program.to.java8.defaultmethod;

public interface InterfacePriorJava8 {

 int method1();

 int method2();

}


This is a crucial interface and providing this to our clients. Assuming that we have 1000+ clients and all have been implemented these two methods in it as below.

package com.java.w3schools.blog.java.program.to.java8.defaultmethod;

public class ImplementationClass1 implements InterfacePriorJava8 {

 @Override
 public int method1() {
  System.out.println("class 1: method 1");
  return 0;
 }

 @Override
 public int method2() {
  System.out.println("class 1: method 2");
  return 0;
 }

}


public class ImplementationClass2 implements InterfacePriorJava8 {

 @Override
 public int method1() {
  System.out.println("class 2: method 1");
  return 0;
 }

 @Override
 public int method2() {
  System.out.println("class 2: method 2");
  return 0;
 }

}


public class ImplementationClass3 implements InterfacePriorJava8 {

 @Override
 public int method1() {
  System.out.println("class 3: method 1");
  return 0;
 }

 @Override
 public int method2() {
  System.out.println("class 3: method 2");
  return 0;
 }

}


..
..
..
..

public class ImplementationClass1000 implements InterfacePriorJava8 {

 @Override
 public int method1() {
  System.out.println("class 1000: method 1");
  return 0;
 }

 @Override
 public int method2() {
  System.out.println("class 1000: method 2");
  return 0;
 }

}

All of the above classes are completely valid because they all implemented both methods method1() and method2().

After some time, we realized that some of the clients need certain operations so now we planned to add the new method method3() in the interface InterfacePriorJava8 which has already two abstracts methods.

Adding new method method3().

public interface InterfacePriorJava8 {

 int method1();

 int method2();
 
 int method3();
}

After adding method3(), now all 1000 implementation classes must have to implement this third method. That means all these classes are affected and will not compile until adding the implementation method() with a valid logic or empty implementation. But, in the real world application, we can not impose this on all the clients.

Compile-time error:

The type ImplementationClass1 must implement the inherited abstract method InterfacePriorJava8.method3()

3. Default Methods in Java 8


So before java 8, it is impossible to extend the functionality of an existing interface without effecting implementation classes. JDK 8 Engineers addressed this issue and provided a solution in the form of Default methods, which are also known as Defender methods or Virtual Extension Methods.

So now changing the method3() to default and all implementation classes now no need to provide the implementation to method3() because it is declared as default so that all implementation classes have direct access to it. If the default behavior is not related then we need to override it.

public interface InterfacePriorJava8 {

 int method1();

 int method2();

 default int method3() {
  System.out.println("method3 is a default method.");

  return 1;
 }
}

Read complete article on "java 8 API default methods and static methods inside interfaces".

4. Conclusion


In this article, we have understood what is the significance of default methods in java 8 with a scenario-based explanation.

Hence, The main advantage of Default Methods inside interfaces is without effecting implementation classes we can extend the functionality of interface by adding new methods (Backward compatibility).




No comments:

Post a Comment

Please do not add any spam links in the comments section.