default and static method in Java Interface
UPDATED: 09 June 2015
Tags:
Interface
In my previous article I explained Difference between Abstract class and Interface in Java. It gives you insight on interface in Java. Now Java designer introduced default and static methods in interface with Java 8. Now you can provide default implementation in Interface.
Why default and static method introduced?
Java designer has to add new methods to existing Java interfaces. For example if Java designer add new method in Iterable interface and you are implementing Iterable interface in one of your class then you've to override that method in your class and if you don't override that method then it'll break your code Java 8 environment.
Considering this one single change in existing Java interface would stop billions of program so they introduced default and static methods in Interface.
Source code(Mobile Interface)
import java.util.TimeZone; public interface Mobile { void voiceCall(); void sms(); /* Java 8: static method */ static TimeZone getTimeZone() { return TimeZone.getDefault(); } /* Java 8: default method */ default String deviceLocator(boolean enabled) { if (enabled) { return "New York"; } else { return "Siberia"; } } }
Source code(Using Interface)
import java.util.TimeZone; public class Motorola implements Mobile { @Override public void voiceCall() { System.out.println("voice call"); } @Override public void sms() { System.out.println("sms"); } public static void main(String[] args) { Mobile mobile = new Motorola(); /* Call voiceCall method */ mobile.voiceCall(); /* Call sms method */ mobile.sms(); /* Call default method deviceLocator */ System.out.println(mobile.deviceLocator(true)); /* Call static method getTimeZone */ System.out.println(Mobile.getTimeZone().getDisplayName()); } }As you can see its not compulsory to implement deviceLocator and getTimeZone methods in class Motorola.
default method
- Its not compulsory to implement.
- You can @Override default method.
static method
- You can consider static method like utility method and also its not compulsory to implement.
- You can't @Override static method.
Two interface with same default method name
What will be the behavior of class when you have two interface with same default method name implemented by one class? Lets understand it with example.
Source code(GPS Interface)
public interface GPS { /* Java 8: default method */ default String deviceLocator(boolean enabled) { if (enabled) { return "China"; } else { return "Siberia"; } } }
Source code(Using Mobile and GPS Interface)
When you implement two interface(Mobile, GPS) in class Motorola which contains same method name then its compulsory for you to @Override default method deviceLocator.
public class Motorola implements Mobile, GPS{ @Override public String deviceLocator(boolean enabled) { //GPS.super.deviceLocator(enabled); //Calling specific default method of interface return "India"; } }
Java 8
Functional Interface in Java 8
Tags:
Interface
0 comments :