Java Interface Default Methods: A New Feature in Java 8, Interfaces Can Have Default Implementations

Traditional interfaces (before Java 8) only allowed defining abstract methods, requiring all implementing classes to manually add new methods, resulting in poor extensibility. Java 8 introduced **default methods** (modified by `default` and provided with concrete implementations) to solve this problem. Default methods have a simple syntax. Interfaces can provide default behaviors, and implementing classes are not forced to override them (e.g., `sayGoodbye()` in the `Greeting` interface). However, they can override the methods as needed (e.g., `ChineseGreeting` overrides `sayGoodbye()`). If multiple interfaces contain default methods with the same name and parameters, the implementing class must explicitly override them; otherwise, a compilation error occurs (e.g., conflicting `method()` in interfaces A and B). The significance of default methods: They allow interface extension without breaking existing implementations, enabling interfaces to combine "contractual nature" and "extensibility." They also avoid the single inheritance limitation of abstract classes and enhance the practicality of interfaces.

Read More