In Java 8, interfaces were enhanced with the addition of default and static methods. These methods were introduced to provide additional functionality to interfaces without breaking the existing code. In this article, we will discuss default and static methods in interfaces with examples.
Default Methods
Before Java 8, interfaces could only contain abstract methods, which meant that any class that implemented the interface was required to implement all of the methods declared in the interface. This made it difficult to add new methods to an interface without breaking the existing implementations. Default methods were introduced to solve this problem.
A default method is a method in an interface that has a default implementation. It is declared using the default
keyword. The implementation of a default method is provided within the interface itself, and is automatically available to any class that implements the interface.
Here is an example interface with a default method:
public interface MyInterface {
void doSomething();
default void doSomethingElse() {
System.out.println("Doing something else");
}
}
In the above example, doSomething()
is an abstract method, while doSomethingElse()
is a default method with a default implementation that prints a message to the console.
Now let’s create a class that implements this interface:
public class MyClass implements MyInterface {
public void doSomething() {
System.out.println("Doing something");
}
}
Since MyClass
implements MyInterface
, it is required to implement doSomething()
. However, since doSomethingElse()
is a default method with a default implementation, MyClass
can choose to override it or use the default implementation. Here’s an example:
MyClass myObject = new MyClass();
myObject.doSomething(); // Output: Doing something
myObject.doSomethingElse(); // Output: Doing something else
As you can see, doSomething()
is implemented in MyClass
, while doSomethingElse()
uses the default implementation provided by MyInterface
.
Static Methods
Static methods were also introduced in Java 8 to provide utility methods that can be used by classes that implement the interface. A static method is a method in an interface that has a body and is marked with the static
keyword.
Here is an example interface with a static method:
public interface MyInterface {
void doSomething();
default void doSomethingElse() {
System.out.println("Doing something else");
}
static void doSomethingStatic() {
System.out.println("Doing something static");
}
}
In the above example, doSomethingStatic()
is a static method with a default implementation that prints a message to the console.
To use a static method, you can simply call it using the interface name:
MyInterface.doSomethingStatic(); // Output: Doing something static
Static methods can also be used within the implementation of the interface itself:
public interface MyInterface {
void doSomething();
default void doSomethingElse() {
doSomethingStatic(); // Call the static method
System.out.println("Doing something else");
}
static void doSomethingStatic() {
System.out.println("Doing something static");
}
}
Conclusion
Default and static methods in interfaces are powerful features that were added in Java 8. Default methods allow for the addition of new methods to an interface without breaking the existing code, while static methods provide utility methods that can be used by classes that implement the interface. By using these features, you can write more flexible and reusable code in Java.