views
Introduction to Interfaces in Java
Before Java 8, interfaces in Java were limited to abstract methods—methods without a body. This meant that if you wanted to add a new method to an interface, every class implementing that interface had to provide an implementation for it. This limitation created challenges, especially when updating large systems.
For example, if you had a widely-used interface and wanted to add a new method, all implementing classes would break unless you updated each one. Java 8 introduced the default method to solve this issue.
What Is a Default Method in Java?
A default method is a method defined within an interface that has a body. It uses the default keyword and allows developers to add new functionalities to interfaces without breaking the implementing classes.
Syntax:
public interface MyInterface {
default void myDefaultMethod() {
System.out.println("This is a default method.");
}
}
The default keyword ensures that this method has an implementation, making it optional for implementing classes to override it.
Why Were Default Methods Introduced?
Java 8 introduced default methods mainly to address the issue of evolving interfaces in backward-compatible ways. Here are the primary reasons:
1. Interface Evolution
Without default methods, evolving an interface required updating every class that implements it. Default methods allow you to add new features without changing current systems.
2. Multiple Inheritance of Behavior
Java does not allow a class to inherit from more than one class because it can lead to confusion. However, interfaces can now provide method implementations, allowing multiple inheritance of behavior without conflicts.
3. Backward Compatibility
Default methods allow old interfaces and implementations to keep working.
Real-Life Use Case: The List Interface
One great example of default method usage can be seen in the Java Collections Framework. The List interface has a method:
default void forEach(Consumer<? super T> action)
This method was added in Java 8, and since it's a default method, existing implementations of List didn’t need any changes.
How Default Methods Work
A default method behaves like an instance method. If the implementing class does not override it, the default implementation will be used.
Here’s a complete example:
interface Vehicle {
default void start() {
System.out.println("Car is starting");
}
}
class Car implements Vehicle {
There is no need to change the start() function.
}
public class Test {
public static void main(String[] args) {
// your code goes here
}
Car myCar = new Car();
car.start(); // Output: Car is starting
}
}
Overriding Default Methods
You can override default methods in the implementing class if you need a different behavior.
class Bike implements Vehicle {
@Override
public void start() {
System.out.println("Bike is starting");
}
}
Multiple Inheritance and Conflict Resolution
If a class implements two interfaces that have the same default method, it must override the method to resolve the conflict.
Example:
interface A {
default void greet() {
System.out.println("Hi from A");
}
}
interface B {
default void greet() {
System.out.println("Hi from B");
}
}
class C implements A, B {
public void greet() {
// Resolving conflict
A.super.greet();
}
}
Rules and Restrictions of Default Methods
-
You cannot create default methods in classes—only in interfaces.
-
You cannot define a default method for methods of Object class (like equals, hashCode, toString).
-
A class implementing multiple interfaces with the same default method must override it.
Best Practices for Using Default Methods
-
Use Sparingly: Default methods are useful for backward compatibility, not as a substitute for class inheritance.
-
Avoid State: Default methods should not manage state; they’re meant for behavior sharing.
-
Document Clearly: Explain why the default method exists and how it can be overridden.
-
Consistent Behavior: Ensure default methods follow consistent behavior patterns to avoid unexpected results.
Avoid Complex Logic: Keep logic minimal; default methods are not the place for complex algorithms.
READ MORE: https://www.viharatech.com/what-is-default-method-in-java-explained-clearly/


Comments
0 comment