In Part 1 of this article, we had taken a look at interfaces in their traditional form. Traditionally, interfaces could only define abstract methods that help to specify the behavior of a class. Java 8 has made many improvements to interfaces. Since Java 8, you can have methods with method bodies in an interface. In this article, we will be taking a look at the new features added to interfaces by Java 8 and Java 9.
- Default Interface Methods
- Static Interface Methods
- Benefits of Static and Default Methods
- Difference between Default and Static Methods
- Private Interface Methods
- Difference between Abstract classes and interfaces after Java 8
Default Interface Methods
Java 8 introduced a feature called default methods in interfaces. Default methods have method bodies that provide a default implementation for the interface method. Java 8 introduced a new keyword called default that needs to be specified with the method declaration in order to define a default method.
Sample Code
public interface Interface1 { void abstractMethod(); default void defaultMethod1() { System.out.println("defaultMethod1 in Interface1"); } default void defaultMethod2() { System.out.println("defaultMethod2 in Interface1"); } }
- This code specifies an interface called Interface1
- Line 2 specifies an abstract method called abstractMethod
- Line 3 specifies a default method called defaultMethod1
- Line 6 specifies a default method called defaultMethod2
A class that implements the interface may override a default method in the interface but it is not mandatory. In such situations, the implementation in the interface is used.
Sample Code
public class Class1 implements Interface1{ public void abstractMethod() { System.out.println("abstractMethod in Class1"); } public void defaultMethod1() { System.out.println("defaultMethod1 in Class1"); } public static void main(String[] args) { Class1 obj1 = new Class1(); obj1.abstractMethod(); obj1.defaultMethod1(); obj1.defaultMethod2(); } }
- This code specifies a class called Class1 that implements Interface1
- Line 2 provides an implementation for the abstract method abstractMethod defined in Interface1
- Line 5 provides an implementation for the default method defaultMethod1 defined in Interface1
- Although no implementation is provided for defaultMethod2 in Interface1, no compilation error occurs.
- Line 8 specifies a main method
- Lines 9 creates an object obj1 of Class1
- Line 10 invokes the abstract method abstractMethod
- Line 11 invokes the default method defaultMethod1. Since an implementation is provided in Class1, this implementation is used
- Line 12 invokes the default method defaultMethod2. Since an implementation is not provided in Class1, the implementation in the interface is used.
Output
abstractMethod in Class1
defaultMethod in Interface1
Static Interface Methods
In addition to default methods, an interface can also contain static methods. Static methods are also methods with method bodies. However, instead of the keyword default, they have the keyword static specified.
Sample Code
public interface Interface1 { void abstractMethod(); static void staticMethod() { System.out.println("staticMethod in Interface1"); } }
- This code specifies an interface called Interface1
- Line 2 specifies an abstract method called abstractMethod
- Line 3 specifies a static method called staticMethod
Unlike default methods, static interface methods cannot be overridden by the class that implements the interface. Also, they need to be invoked via the interface name.
Sample Code
public class Class1 implements Interface1{ public void abstractMethod() { System.out.println("abstractMethod in Class1"); } public static void main(String[] args) { Class1 obj1 = new Class1(); obj1.abstractMethod(); Interface1.staticMethod(); } }
- This code specifies a class called Class1 that implements Interface1
- Line 2 implements the abstractMethod defined in Interface1
- Line 5 specifies a main method
- Lines 6 creates an object obj1 of Class1
- Line 7 invokes the abstract method abstractMethod via obj1
- Line 8 invokes the static method staticMethod via the interface name, that is Interface1.
- If you try to invoke staticMethod via obj1, this will cause a compilation error.
Output
abstractMethod in Class1
staticMethod in Interface1
Benefits of Static and Default Methods
Static and default methods have several benefits. These are as follows:
- Default methods are used to achieve backward compatibility with code. So, if a new method is added to an interface, it will break the code of all the classes that implement the interface. Having a default implementation in the interface prevents this.
- Java 8 added a new feature called for-each which allows internally iterating over a Collection. A new forEach method is added to an interface called Iterable which is implemented by all the Collection interfaces like Collection, List and Map. The forEach method is given a default implementation in the Iterable interface. This allows all the Collection classes to work without any changes.
- Static interface methods are used to group utility methods.
Difference between Default and Static Methods
Although both default and static interface methods are methods that have method bodies, there are some differences between the two. These are as follows:
- Default interface methods have the keyword default specified; static methods have the keyword static specified
- Default methods can be overridden in the class that implements the interface, static methods cannot be overridden
- Default methods can be invoked via an object of the class that implements the interface, static methods cannot be invoked via an object of the class that implements the interface, they need to be invoked via the interface name.
Private Interface Methods
Static and default interface methods were added by Java 8. Java 9 introduced private interface methods. Private interface methods are also method with method bodies. They have the keyword private specified in the method declaration. They are used to contain code that is private to the interface, that is code that the interface does not wish to expose to the outside world. Private interface methods are typically used to reuse code in an interface. So if an interface has several static/default methods that all share some common functionality, the common code can be placed in a private method and the private method can be invoked from within the static/default methods.
Sample Code
public interface Interface1 { default void defaultMethod1() { privateMethod(); System.out.println("In defaultMethod1"); } default void defaultMethod2() { privateMethod(); System.out.println("In defaultMethod2"); } private void privateMethod() { System.out.println("Private Method"); } }
- This code specifies an interface called Interface1
- Line 2 specifies a default method called defaultMethod1
- Line 6 specifies a default method called defaultMethod2
- Line 10 specifies a private method called privateMethod.
- The default methods defaultMethod1 and defaultMethod1 invoke the private method
Difference between Abstract classes and interfaces after Java 8
Default, static and private interface methods allow having concrete as well as abstract methods in an interface. So, this has made interfaces quite similar to abstract classes which have both concrete and abstract methods. However, there are still some important differences between abstract classes and interfaces as follows:
- An interface cannot have a constructor, an abstract class can have a constructor
- An abstract class can have instance fields. An interface on the other hand cannot have instance fields. So, interfaces are mainly used to define a behavior that a class must provide and that does not change with default/static and private methods.
Conclusion
So, in this article, we learnt all about the new features added by Java 8 and Java 9 to interfaces. We understood about default, static and private interface methods.