Interfaces in JAVA a deeper look

Jatharthan
4 min readJun 19, 2021

Interface is a blueprint for a class, ensures the class implements it provides all the services an interface promised to provide to the outside world. It is introduced in java for mainly achieving three concepts.

  1. To provide abstraction.

The implementation of how services are provided is hidden from the outside and only what services are provided is exposed.

2. To Achieve multiple inheritance

Java restrict multiple inheritance to avoid diamond problem caused by ambiguity that may rise if two parent classes have a method with same signature ,Compiler can’t decide on which method to call in this situation. This is solved by interfaces since all abstract methods should be implemented by the concrete class implementing it and also can decide on which default method to be called if ambiguity is raised by default methods(discussed later in the article)

3. To achieve loose coupling

Since interfaces can be used as type, loose coupling can be achieved so that no need to instantiate the object directly inside a dependent class. User can decide on which implementation of that interface type needed.

Evolution of interface

Before JAVA SE 8 interfaces contain only abstract methods which are public and abstract by default and constants which are public static final by default.

After JAVA SE 8 interfaces can have default and static methods which should be implemented in the interface it self. both are public by default.

After JAVA SE 9 private and private static methods allowed.

Why default methods?

In later stages if we would like to add new method signature inside interface all the classes that already implemented this interface break. Default methods solve this since they have implementation and some restrictions to avoid diamond problems. The classes which needs this default method can directly use it or can override it. By this JAVA maintains it backward compatibility.

How diamond problem caused by default methods is resolved?

  1. at event of signature conflict between a superclass and interface’s default method, superclass method take priority.
  2. A sub type interface’s default method takes priority over super type interface’s default method at event of signature conflict.
  3. At event of signature conflict when implementing two or more interfaces the concrete class should have to override the default method and it can decide on which interface default method to be called or can have its own implementation.

Why static method?

As normal classes in java static method is available only in the the static context. It is used to group similar or related utility methods. private and public static method can be included.

Why private methods?

To increase code re-usability and to improve encapsulation. it can be accessed only by default and other private methods inside interface. static and non-static context also should be considered when deciding accessibility.

Interfaces can have nested types such as classes, enum and interfaces.

Interfaces can’t have instance variables, constructors, protected access modifier.

Marker interfaces

A marker interface is an interface that has no methods or constants. It is just an empty interface. It provides run-time type information about objects. it satisfy the instanceOf method. it is also called as Tagged interface

Functional interfaces

A functional interface is an interface that contains only one abstract method. It can contain default static and private methods. It is introduced in java 8 to support lambda expressions which is a functional programming concept. Before functional interface normally concrete class have to implement all abstract methods in the interface even it don’t need it. Now since functional interface have only one abstract method the classes can implement only needed interfaces. @FunctionalInterface annotation is used to ensure that functional interfaces can have only one abstract method. Though this annotation is not mandatory it is advised to use this to avoid accidently putting extra abstract methods into these interfaces.

The java.util.function package contains many built-in functional interfaces in Java 8 which are mostly used together with lambda expressions.

Abstract class vs interfaces after JAVA SE 9

  • Abstract class can have methods which are abstract and also implemented(non abstract) methods. Interfaces can have abstract methods and default methods.
  • Abstract class can have instance variables, static variables and constants. Interfaces can have constants only which are public static final by default.
  • Abstract class doesn’t support multiple inheritance. Interface supports multiple inheritance.
  • Members of abstract class can have public protected private and can left with default package level access. Interface abstract methods and default methods are public by default. private and static methods are also allowed.
  • Abstract class can extend another java class and implements several interfaces. Interface can extend only one interface.

Conclusion

Interfaces in java evolved over each versions to solve various issues. Interfaces provide a great abstraction, support multiple inheritance and support to achieve loose coupling. Functional interfaces are introduced to support functional programming concept. Study each of this topic by having code samples in hand for better understanding.

--

--