In this tutorial I am going to show how default and static methods can be included into Java 8 interface. Prior to Java 8 we did not have such mechanism to include default method or static method into interface but Java 8 provides such option to include those methods. Static methods are regarded as utility methods, not as essential, core methods. Whereas default methods are treated as common behaviors for all the client classes.

When you extend an interface that contains a default method, you would get the following benefits:

  • If you do not mention the default method at all, which lets your extended interface inherit the default method.
  • If you re-declare the default method, which makes it abstract.
  • If you redefine the default method, which overrides it.

You can fine more info here https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Example on default and static methods in interface

package intrface;

import java.time.DateTimeException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public interface InterfaceTest {

    LocalDateTime getLocalDateTime();

    static ZoneId getZoneId(String zoneString) {
        try {
            return ZoneId.of(zoneString);
        } catch (DateTimeException e) {
            System.err.println("Invalid time zone: " + zoneString + "; using default time zone instead.");
            return ZoneId.systemDefault();
        }
    }

    static String getMsg() {
        return "Hello, World!";
    }

    default ZonedDateTime getZonedDateTime(String zoneString) {
        return ZonedDateTime.of(getLocalDateTime(), getZoneId(zoneString));
    }

    default String getHello() {
        return "Hello";
    }

}

Client class

package intrface;

import java.time.LocalDateTime;

public class InterfaceTestClient implements InterfaceTest {
    @Override
    public LocalDateTime getLocalDateTime() {
        return LocalDateTime.now();
    }
}

Usage

package intrface;

public class TestInterfaceTestClient {

    public static void main(String[] args) {
        InterfaceTest interfaceTest = new InterfaceTestClient();
        System.out.println("Current Time: " + interfaceTest.getLocalDateTime());
        System.out.println("Zonal Time: " + interfaceTest.getZonedDateTime("Asia/Kolkata"));
        System.out.println("getHello: " + interfaceTest.getHello());
        System.out.println("getZoneId: " + InterfaceTest.getZoneId("Asia/Kolkata"));
        System.out.println("getMsg: " + InterfaceTest.getMsg());
    }

}

Output

Current Time: 2017-05-26T08:41:15.009
Zonal Time: 2017-05-26T08:41:15.011+05:30[Asia/Kolkata]
getHello: Hello
getZoneId: Asia/Kolkata
getMsg: Hello, World!

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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