Over a million developers have joined DZone.

Java 8 History: Interfaces

DZone's Guide to

Java 8 History: Interfaces

This look back into Java 8 considers what the version brought to interfaces, including primers for default methods and rules for multiple inheritance conflicts.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

Interfaces in Java are usually a mechanism for allowing a number of classes to share a number of methods and constants. It is also one of the best mechanisms for achieving polymorphism in Java.

So if you are one of those types that were very familiar with Interfaces before the arrival of Java 8, it would make a great deal of sense to discover some of the cool stuff Interfaces could do when Java 8 arrived.

Interfaces prior to Java 8 used to contain only abstract methods and constants, and any class that implemented an Interface must implement all its methods or be declared abstract. Well, this was before Java 8. With Java 8, that was upgraded.

Interfaces can, post-Java 7, contain methods with implementations. Such methods that have an implementation right from the interface were called default methods. These allow for a default implementation of a method by all classes that implement an interface and provides the opportunity for the implementing classes to override the method, if they so wish.

public interface Bank{
    default String getBankName(){
        return “Azibit Bank”;

Note the syntax for the default method. It must be preceded by the default keyword.

With this introduction, the major concern on the mind of every Java developer is, "How did this concept manage multiple inheritances from different interfaces or if a class and an interface have the same method?"

Well, such fear was anticipated, so the following rules applied when more than one interface had the same method and a class implements the two of them.

  1. A class implementation of a method takes precedence over a default method. So if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect.
  2. However, if two interfaces implement the same default method, then there is a conflict.
  3. In cases where one interface inherits another interface and both of them implement a default method, the default method of the child interface would be used by an implementing class.
  4. Also, an explicit call to an interface default method can be made from inside an implementing class using super. For example Interface.super.defaultMethod()

Also, Java 8 introduced static methods in Interfaces. So interfaces could create static methods and reference these static methods from anywhere simply by calling the Interface name followed by the method name. For example: InterfaceName.staticMethodName()

To create a static method, you would need to add the static keyword when creating the method.

public interface Test{ 
    static String getStaticNumber(){ 
        return 1; 

Reference: Java The Complete Reference, By S. Herbert

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

java ,interface ,default methods in interface ,static method in interface ,tutorial

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}