Default Methods in Java 8
Overview
Default methods, introduced in Java 8, allow developers to add new methods to interfaces without breaking the existing implementations. This feature provides a way to add new functionalities to interfaces while maintaining backward compatibility.
Defining Default Methods
A default method is defined in an interface with the default
keyword, followed by a method body.
Example: Defining a Default Method
interface MyInterface { void existingMethod(); default void newMethod() { System.out.println("New default method"); } }
Using Default Methods
Classes that implement an interface with default methods can use the default methods without the need to override them, although they can choose to override if needed.
Example: Using a Default Method
class MyClass implements MyInterface { public void existingMethod() { System.out.println("Existing method implementation"); } } public class DefaultMethodExample { public static void main(String[] args) { MyClass myClass = new MyClass(); myClass.existingMethod(); myClass.newMethod(); } }
Multiple Inheritance with Default Methods
Default methods enable a form of multiple inheritance. If a class implements multiple interfaces that have default methods with the same name, the class must override the method to resolve the conflict.
Example: Resolving Method Conflict
interface InterfaceA { default void commonMethod() { System.out.println("InterfaceA commonMethod"); } } interface InterfaceB { default void commonMethod() { System.out.println("InterfaceB commonMethod"); } } class MyClass implements InterfaceA, InterfaceB { public void commonMethod() { InterfaceA.super.commonMethod(); InterfaceB.super.commonMethod(); System.out.println("MyClass commonMethod"); } } public class MultipleInheritanceExample { public static void main(String[] args) { MyClass myClass = new MyClass(); myClass.commonMethod(); } }
Extending Interfaces with Default Methods
Interfaces that extend other interfaces can also include default methods. This allows for more flexible and extensible interface hierarchies.
Example: Extending Interfaces with Default Methods
interface ParentInterface { default void parentMethod() { System.out.println("ParentInterface parentMethod"); } } interface ChildInterface extends ParentInterface { default void childMethod() { System.out.println("ChildInterface childMethod"); } } class MyClass implements ChildInterface { // No need to override parentMethod or childMethod } public class ExtendedInterfaceExample { public static void main(String[] args) { MyClass myClass = new MyClass(); myClass.parentMethod(); myClass.childMethod(); } }
Advantages of Default Methods
- Backward Compatibility: Allows new methods to be added to existing interfaces without breaking existing implementations.
- Multiple Inheritance: Enables a form of multiple inheritance, allowing classes to inherit behaviors from multiple interfaces.
- Code Reusability: Provides a way to reuse common method implementations across multiple classes.
Conclusion
Default methods in Java 8 provide a powerful mechanism for evolving interfaces and adding new functionalities without breaking existing code. They offer flexibility, enhance code reusability, and enable a form of multiple inheritance.