Default methods (default)
Introduced in Java 8, default methods allow interfaces to provide concrete method implementations without breaking existing code. They are defined with the default keyword.
Purpose:
- Evolve interfaces by adding new methods without forcing all implementing classes to provide implementation.
- Provide common behavior that can be overridden if needed.
Syntax:
interface Vehicle { void start();
default void honk() { System.out.println("Beep beep!"); }}Using default methods:
class Car implements Vehicle { @Override public void start() { System.out.println("Car starting"); } // honk() is inherited (can be overridden optionally)}
Car car = new Car();car.start(); // Car startingcar.honk(); // Beep beep!Overriding default methods:
class ElectricCar implements Vehicle { @Override public void start() { System.out.println("Electric car silently starts"); } @Override public void honk() { System.out.println("Electric beep"); }}Conflict resolution: When a class implements multiple interfaces with conflicting default methods, the class must override the method. Use InterfaceName.super.methodName() to call a specific default.
Note: Default methods are not allowed to override Object class methods (e.g., equals, hashCode).