Default and Static Methods in Java 8 Interface – Features & Use Cases
A default method is a method define in an interface with a default implementation .it uses the default keyword and allows interfaces to have behavior without forcing all implementing classes to override it .
Syntax:
interface Vehicle
{
void start();
default void fuelType(){
System.out.println("petrol");
}
}
The main purpose of default methods is to enable backward combability when evolving interfaces before java8 , adding a new method to an interface would break all implementing classes with default methods, you can add new functionality without affecting existing code .
Features of Default Methods in Java
- Method with Implementation in Interface
Default methods allow you to add method implementations directly inside an interface using thedefaultkeyword. - Backward Compatibility
Existing classes that implement the interface do not need to override the new default method, preventing breaking changes. - Supports Multiple Inheritance of Behavior
A class can inherit default methods from multiple interfaces, enabling flexible design. - Can Be Overridden
Implementing classes can override default methods to provide custom behavior. - Part of Java 8 Functional Programming
Default methods support functional programming by working alongside lambda expressions and functional interfaces.
Interface with default method
- Inside Interface every variable is always public, static and final . we can not declare instance variable .
- Interface never talks about state of object.
- Inside interface we can not declare constructor .
- Inside Interface we can not declare instance and static blocks .
- Functional interface with default method can refer lambda expressions .
- Inside interface we can not override object class methods .
Abstract class
- Inside abstract class we can declare instance variables , which are required to the child class .
- Abstract class can talk about state of object .
- Inside Abstract class we can declare constructor .
- we can declare instance and static block .
- Abstract class Can not refer Lambda expressions .
- Inside Abstract class we can override object class Method .
Use Cases of Default Methods in Java
- Adding New Features to Existing Interfaces
When you need to extend an interface without breaking older implementations. - Providing Common Utility Methods
Useful for logging, validation, or helper methods shared by multiple classes. - Framework & Library Development
Widely used in Java collections, streams, and APIs for smooth version upgrades. - Supporting Multiple Inheritance of Behavior
When a class needs to inherit functionality from multiple interfaces.
What is Static Method in an Interface
A static method in an interface is a method that belongs to the interface itself , not to the instances of the classes that implement it . it is declared using the static keyword and can be called using the interface name .
Syntax :
Interface Utility {
static void printMessage(String msg){
System.out.println("Message :"+msg);
}
}
Interface static methods always you can call by using interface Name only .
Utility.printMessage("Hello from static method !")
Purpose of static methods in interfaces
1.Utitility or Helper Methods
Interfaces can now include related utility methods without needing a separate utility class .
2.Encpsulation of logic
Static methods allow encapsulating logic that is specific to the interface and not meant to be overridden by implementing classes .
Interface Static methods with respect to overriding
Interface static method by default not available to the implementation class . that’s why overriding concept not applicable for static method .
interface parentinterf
{
public static void m1()
{
}
}
public class childInterf implements parentInterf
{
public static void m1()
{
}
}
above code compile successful because static method not participating in overriding .