Java 101More about Interface in Java 8

time to read 4 min | 695 words

Interface was meant to define a contract before Java 8, where we were able to define the methods a class needed to implement if binding himself with the interface. Interface was only involved with abstract methods and constants.

But in Java 8, Interface has become much more, and now it can have methods defined using static or default. Remember that default methods can be overridden, while static methods cannot.

Method Definitions

Interface was meant for good designers because when we create an Interface, we should know the possible contract that every class should implement. Once your contract definition is done and classes have implemented the contract, it’s difficult to change the definition of an Interface, as it will break the implemented classes.

A good designer always creates an interface and provides a base class, which provides the default definition of the Interface methods, and classes should extend the base class in place of implementing the interface directly. In this way, any future change in Interface could be taken care of by the base class. Other implementations of sub-classes would be fine.

In Java 8, they tried to fix this issue by providing method definitions — using static or default. We can add the definitions using static or default in Interface without breaking the existing classes, making it easier to design interfaces in Java 8.

Do We Still Need Abstract Classes?

After the Java 8 interface enhancements, the new version of Interface looks like a great replacement for the Abstract class, right? No, not at all. There are still the differences between these two. Do you remember ‘access specifiers’ (public, protected, etc.)?

So the Abstract class can have any of these access specifiers for their methods and variables while an Interface is always public and variables in an Interface are always constants. So we need to be very wise when choosing between the Interface and Abstract classes, and I believe that some intelligence is still required.

Examples of Interface Enhancements

We can define an Interface as having static and default methods as below:

Java8Interface.java:

package id.rezhajulio.interface.enhancement;

public interface Java8Interface {

    //defaults method - by default it's public

    default void hi() {

        System.out.println("In Java8Interface: new feature of Java8 is saying Hi....");

    }

    //static method - by default it's public

    static void hello() {

        System.out.println("In Java8Interface: new feature of Java8 is saying Hello....");

    }

}

The concrete class that is implementing the above Interface can be used as below.

ConcreteClass.java:

package package id.rezhajulio.interface.enhancement;

public class ConcreteClass implements Java8Interface {

    public static void main(String[] args) {

        ConcreteClass c = new ConcreteClass();

        c.hi(); // would be accessible using class instance

        //Not possible to call using class instance

        Java8Interface.hello();

    }

}

Which will lead to this output:

In Java8Interface: new feature of Java8 is saying Hi....

In Java8Interface: new feature of Java8 is saying Hello....

But suppose you have two interfaces with the same method signature as ‘default’. Then, your class should define the implementation for that method. Otherwise, you’ll see a compile time error.

Let’s see how that plays out below. The following Interface has the same methods as our Java8Interface.

Java8Interface_1.java:

package id.rezhajulio.interface.enhancement;

public interface Java8Interface_1 {

    default void hi() {

        System.out.println("In Java8Interface_1: new feature of Java8 is saying Hi....");

    }

    static void hello() {

        System.out.println("In Java8Interface_1: new feature of Java8 is saying Hello....");

    }

}

Meanwhile, our concrete class handles the implementation for our default method as follows:

ConcreteClass.java:

package id.rezhajulio.interface.enhancement;

public class ConcreteClass implements Java8Interface, Java8Interface_1 {

    public static void main(String[] args) {

        ConcreteClass c = new ConcreteClass();

        c.hi();

        Java8Interface.hello();

    }

    //We need to override hi method otherwise compilation error

    @Override

    public void hi() {

        Java8Interface.super.hi();

    }

}

And our output will be as follows:

In Java8Interface: new feature of Java8 is saying Hi....

In Java8Interface: new feature of Java8 is saying Hello....

Next, say a List API has the same default implementation as below:

default void sort(Comparator c))

That helps call the sort methods on our List, and no more Collections API is needed.

And let’s see the snippet for calling the sort method using a List API:

List list = new ArrayList<>(); list.add("v");

list.add("a");

list.add("z"); list.add("d");

list.sort((val1, val2) -> val1.compareTo(val2));

Enjoy the power of Interface!


This is a post in the Java 101 series.
Other posts in this series:

  1. Apr 29, 2017 Translating Scanner tokens into primitive types
  2. Apr 28, 2017 Listing a file system's root directories
  3. Apr 27, 2017 The Console class
  4. Apr 20, 2017 Using an interface as a parameter
  5. Apr 19, 2017 Using bounded type parameters in generic methods
  6. Apr 18, 2017 Using the Deprecated annotation
  7. Apr 17, 2017 Diamond Operator in Java
  8. Apr 15, 2017 Altering format string output by changing a format specifier's argument_index
  9. Apr 13, 2017 Future of Interface in Java 9
  10. Apr 12, 2017 More about Interface in Java 8
  11. Apr 10, 2017 Manage Your JVM Environment with SDKMAN
  12. Apr 09, 2017 Updating interfaces by using default methods
  13. Apr 08, 2017 Converting Stacktrace to String

Related Post:

  1. Apr 29, 2017 Translating Scanner tokens into primitive types
  2. Apr 28, 2017 Listing a file system's root directories
  3. Apr 27, 2017 The Console class
  4. Apr 20, 2017 Using an interface as a parameter
  5. Apr 19, 2017 Using bounded type parameters in generic methods
  6. Apr 18, 2017 Using the Deprecated annotation
  7. Apr 17, 2017 Diamond Operator in Java
  8. Apr 15, 2017 Altering format string output by changing a format specifier's argument_index
  9. Apr 13, 2017 Future of Interface in Java 9
  10. Apr 11, 2017 Spring Boot in a Single File
  11. Apr 10, 2017 Manage Your JVM Environment with SDKMAN
  12. Apr 09, 2017 Updating interfaces by using default methods
  13. Apr 08, 2017 Converting Stacktrace to String
  14. Apr 07, 2017 Synchronized Statement in Java
  15. Jul 12, 2016 Fix Java Unsupported major.minor version 52.0 on Ubuntu