Default and Static Methods in Interface Java 8

In the Java programming language, an interface is a reference type. Interfaces are similar to a class. It can contain only constants, method signatures, default methods, static methods, nested types and private methods (introduced in Java 9).

Method bodies exist only for default methods and static methods (private methods are introduced in Java 9). Note that interfaces cannot be instantiate. It can only be implemented by classes or extended by other interfaces.

Java 8 introduces various new features, including lambda expressions, functional interfaces, method references, streams, Optional, and static and default methods in interfaces.

In this section, we will discuss how to use static and default methods in interfaces with Java programs.

Why default methods?

One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions. If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from Java.

Default Methods

Java 8 introduces default methods to help with the problem of changing interfaces. A new method added to an interface before default methods would cause all the classes implementing that interface to break. Interfaces can have additional methods added to them without the implementing classes having to submit an implementation due to default methods.

Default methods are declared using the default keyword in the method signature. This keyword distinguishes default methods from regular abstract methods in interfaces.

Syntax

Use Cases

1. Backward compatibility

Without default methods, adding a new method to an interface that already has several implementations may cause issues. By using default methods, you can extend an interface's functionality without affecting already implemented implementations.

2. Diamond Issue

In the event that a class implements two interfaces with competing default methods, the implementing class is required to either select one of the methods explicitly or supply its implementation.

MethodImplementation.java

Output:

Regular method implementation
Default method implementation

Explanation

In this instance, the default method defaultMethod() is present in MyInterface. A concrete implementation of the regular method is provided by the class MyClass, which also implements MyInterface. The default behavior will be passed down to the class if it doesn't supply an implementation for the default function.

Static Methods

Java 8 added static methods to interfaces in addition to default methods. These methods can be invoked on the interface itself without requiring an instance of the interface; they are comparable to static methods in classes. The purpose of adding static methods to interfaces was to give a means of organizing similar utility methods inside the interface. In contrast to default methods, static methods are not included in the interface's API for implementing classes and do not rely on an instance of the interface.

Syntax

Static methods are declared in interfaces using the static keyword. The keyword indicates that the method is associated with the interface itself, not with instances of implementing classes.

Avoiding Utility Classes

Before Java 8, utility methods were often placed in utility classes. With static methods in interfaces, there is no need to create separate utility classes just for related static methods, leading to more cohesive code organization.

Use Cases

1. Practical Techniques

Interfaces with static methods can be used to aggregate utility methods that are associated with the interface's notion. These techniques offer standard features that are not instance-specific.

2. No Inheritance Conflicts

Static methods do not take part in more than one inheritance conflict, in contrast to instance methods. Because static methods are called using the interface name, there is no conflict if a class implements more than one interface with the same static method.

StaticMethod.java

Output:

Result: 8
Square root: 5.0

Explanation

The normal method calculate(int a, int b) and the static method squareRoot(int num) of the MyMath interface are shown in this example. Without first generating an instance of the static method, it can be called directly on the interface.

Conclusion

In conclusion, static methods offer a tidy approach to combining utility methods linked to the interface, doing away with the requirement for distinct utility classes. In contrast, default methods allow interfaces to evolve without affecting currently written code. Both of these characteristics help make Java code designs more adaptable and reusable.






Latest Courses