Default Methods in Java : Making interfaces backward compatible
Subhodip Basu
?Java Backend Developer ? Immediate Joiner ? Java17 ? Java8 ? Spring Boot ? Microservices ? Distributed System ? Exploring new tech Ideas ? Open to onsite opportunities outside India(Asia Pacific, EMEA, Europe)
Interfaces contained method declarations. It did not allow methods with implementations.
You can see an interface with name BankingFeatures
public interface BankingFeatures {
void checkBalance();
void transferMoney();
}
Any class which will implement any interface has to provide an implementation of the method(s) which are declared in interface.
You can see a class BankingWebApp which is implementing the interface BankingFeatures
public class BankingWebApp implements BankingFeatures {
@Override
public void checkBalance() {
System.out.println("Used for checking balance in account of customer");
}
@Override
public void transferMoney() {
System.out.println("Used for transferring money from customer account");
}
}
Interfaces are like contracts
So if there is an application or service provider, it will have certain specific features or services to provide. Example a booking application will enable us to book tickets or a car service provider will provide us the features like car washing, repairing, checking of overall condition of your car.
Those features or functionalities will be added in the interface like a contract.
If single class implements an interface it will not be issue, unless it is implemented in the class.
Consider a particular interface has been implemented by multiple classes.
If there is any additional change in the contract i.e. an additional method is added in interface, it has to be implemented in all the classes which is implementing the interface.
The implementation might not be relevant in terms of functionality to all classes and will .
With the addition of the new feature, the existing classes will have to implement the new feature irrespective of whether it is required or not . Otherwise existing functionality will be broken.
Here comes an enabling feature introduced in Java 8 : Default Methods
领英推荐
Default method enable us to add method implementation in interface using default keyword.
default method_return_type method_name(//method parameters as required) {
//method body
}
If any additional functionality is required to be added in an interface which is already implemented by class?
or
Any feature which can be implemented in the future, we can add it through default method.
public interface BankingFeatures {
void checkBalance();
void transferMoney();
default void BankingWithEyeScan() {
System.out.println("Feature to be implemented");
}
}
Few examples of default methods include :-
Well , as it is expanding the capability of an interface, we should be keeping valid check before adding a default method as to whether the default method actually has logic which will be default behavior for the class which is implementing the interface.
Handling duplicate default methods
Another issue might arise. Say there are multiple interfaces and those multiple interfaces implement a default method with same name and same function definition. Compiler will display exception during compile time for duplicate default method.
public interface PythonApp {
void codeInPython();
default void addTechnicalNotes() {
System.out.println("Used for documentation Purpose");
}
}
public interface JavaApp {
void codeInJava();
default void addTechnicalNotes() {
System.out.println("Used for documentation Purpose");
}
}
To resolve the issue, we can add the implementation of the default method in the class which is implementing the interfaces i.e. override the default method
public class User implements JavaApp,PythonApp {
@Override
void codeInJava(){
System.out.println("Used with java");
}
@Override
void codeInPython(){
System.out.println(""Used with python");
}
default void addTechnicalNotes() {
System.out.println("Added by user");
}
}
Thanks for reading. Keep exploring. Happy learning !!