Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Interface: The Long Journey to Java 9

DZone's Guide to

Java Interface: The Long Journey to Java 9

Take a quick history lesson to see how interfaces have changed from before Java 8, during Java 8, and more recently in Java 9.

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

Interface in Java has evolved over the Java releases, more so in Java 8 and Java 9. In this article, we will look at how Interface was prior to Java 8 and how it has been enhanced in Java 8 and Java 9.

Interface Prior to Java 8

An interface would have one or more abstract methods as shown below:

public interface MyInterface {
    public void doSomething();
}


And its implementation would be:

public class MyImplementation implements MyInterface {

    public void doSomething() {
        System.out.println("Done in MyImplementation");
    }

    public static void main(String[] args) {
        MyImplementation impl = new MyImplementation();
        impl.doSomething();
    }

}


Interface in Java 8

In Java 8, in order to enhance the Collections API to support lambda expressions and new methods, the interface java.util.Collection had to be enhanced. This would mean breaking all that code that was implementing this interface. So they came up with something called default methods in the interface.

So now interfaces could have methods with implementations, thereby providing a scope for the enhancement of the interfaces:

public interface MyInterface {
    public void doSomething();

    public
    default void doTheDefaultThing() {
        System.out.println("Done in a default way!");
    }
}


Interface in Java 9

Even after the default methods, there was a small limitation in the interface — the lack of constructs for sharing code between the default methods.

Java 9 introduced private methods, which facilitate for code sharing between the non-abstract methods in the interface:

public interface MyInterface {
    public void doSomething();

    public default void doTheDefaultThing() {
        System.out.println("Done in a default way!");
        helper();
    }

    private void helper() {
        System.out.println("Calling the helper!!!");
    }
}


And the above interface enhancements in action:

public static void main(String[] args) {
    MyImplementation impl = new MyImplementation();
    impl.doSomething();
    impl.doTheDefaultThing();
}


Conclusion

The JDK team has clearly made the interfaces much more powerful than they were prior to Java 8 and also opened up a way to enhance the interfaces in the libraries without breaking client code.

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,interface ,default methods in interface ,java 9 ,java 8 ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}