Sunday 21 May 2017

Default and Static Method in Interface

In this post we will discuss about default and static method in interface. From Java 8 we can add default and static method with implementation in interface.


1. Default Method:

Before Java 8 we can only have abstract methods inside an interface i.e we can not provide implementation of any method inside an interface.
Also when we try to add a new method in an existing interface we need to override that method in all the classes which implement the interface.

So to solve these two problems Java came up with the concept of default method in interface:
1. From Java 8 on wards we can add a concrete method inside an interface which marked default using default keyword.
2. Classes which have implemented the interface are not mandatory to override the default methods. Default methods are optional to override. If a class doesn't override the default method it will get default behavior as defined in interface.
Example: 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class DefaultMethodExmaple {

 public static void main(String[] args) {
  Display display = new MyDisply();
  display.normalMethod();
  display.myDefaultMethod();
 }

}

interface Display{
 public void normalMethod();
 public default void myDefaultMethod(){
  System.out.println("This is defualt method");
 }
}

class MyDisply implements Display{

 @Override
 public void normalMethod() {
  System.out.println("This is override normal method");
 }
 
}
Above example is quite self explanatory, As we have Display interface which contains one normal and one default method.
As we can see MyDisplay class which has Implemented the interface has only overrided normal method, as default methods are optional to override.
If we run above code you will get output as below:

1
2
This is override normal method
This is defualt method

As we know default methods are optional to override. In above example if we override the default method in Mydisplay class we will get different output(overrided version). Try it once with above code.

Note: 1. You can have n number of default method inside an interface.
2. The main reason behind the introduction of default method in Java 8 is to make your code backward compatible. As we can add methods in existing interface without breaking existing code.

Multiple inheritance issue(Diamond problem):As we know Java doesn't support multiple inheritance in case of classes to avoid famous diamond problem.
But Since we can implement two or more interface and by the introduction of default method diamond problem will now arise for interfaces.

Solution:  To solve multiple inheritance problem (diamond problem) in case of default method, Java 8 has solution that If a class implements two or more interfaces and each interface having same default method(same name and signature) then class has to override the method to avoid confusion. Compiler will show error if we wouldn't override the method.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
interface Display{
 public void normalMethod();
 public default void myDefaultMethod(){
  System.out.println("This is defualt method");
 }
}

interface DisplayTwo{
 public default void myDefaultMethod(){
  System.out.println("This is defualt method");
 }
}
class MyDisply implements Display, DisplayTwo{
 @Override
 public void normalMethod() {
  System.out.println("This is override normal method");
 }
}


2. Static method:

Static method is same as default method except classes which implement the interface can not override static methods at all.
Static methods belong to interface only and all the classes(which are implement interface) have to use same implementation, they can not override it.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class DefaultMethodExmaple {

 public static void main(String[] args) {
  Display display = new MyDisply();
  display.normalMethod();
  //display.myDefaultMethod(); //It will give compilation error.
  Display.myStaticMethod();
 }
}

interface Display{
 public void normalMethod();
 public static void myStaticMethod(){
  System.out.println("This is static method");
 }
}

class MyDisply implements Display{
 @Override
 public void normalMethod() {
  System.out.println("This is override normal method");
 }
}
Above example same as we have discussed for default method except we have used "static" keyword to have static method named "myStaticMethod".
If you uncomment line number 6 you will get compilation error as I said unlike default methods static methods don't belong to class, they belong to interface only.
So correct way to access static method is as we did at line number 7 by using interface name.

Note : 1. If you try to override static method defined in interface, inside class you will get compilation error. But if you remove @Override annotation you won't get any compilation error as both the methods will then co-exist together one will be part of interface and other will be part of class i.e both will be two different method even name and signature will be same.
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
public class DefaultMethodExmaple {

 public static void main(String[] args) {
  Display.myDefaultMethod(); //Interface
  MyDisply.myDefaultMethod(); // Class
 }
}

interface Display{
 public static void myDefaultMethod(){
  System.out.println("This is interface static method");
 }
}

class MyDisply implements Display{
 public static void myDefaultMethod(){
  System.out.println("This is class satic method");
 }
}
Output :
1
2
This is interface static method
This is class satic method

In above code if you put @Override annotation in MyDisplay class then you will get compilation error.

2. If a class implements two or more interface having the same static method i.e. same name and same signature, In this case we should use interface name to resolve which method we want to use.
Static method and default method both are way of adding concrete method inside interface without breaking the existing code but the difference between them is one can overrides default methods but can not override static method.

No comments:

Post a Comment