Default Methods in Java : Making interfaces backward compatible
Thanks to : Modern Java in Action by Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft for logical illustration and valuable insight & Java Documentation by Oracle

Default Methods in Java : Making interfaces backward compatible

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.


No alt text provided for this image



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 :-

  • ?stream method in the Collection interface
  • ?sort method in the List interface?
  • ?Predicate.and and Function.andThen


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 !!

#java #java8 #javadevelopers #default #interface #programming #linkedin

要查看或添加评论,请登录

Subhodip Basu的更多文章

  • Maximizing Your LinkedIn Profile To Accelerate Career Growth

    Maximizing Your LinkedIn Profile To Accelerate Career Growth

    1. ?????? ?????????????? ???????????????????? ??????????????, ?????????????? ???? ???????????????? ?????????????? ??…

    3 条评论
  • Streams in Java : Introducing declarative style in Java

    Streams in Java : Introducing declarative style in Java

    Collections in java are one of the most regularly used concept in our daily programming Considering this scenario…

  • Reduce Method in Java

    Reduce Method in Java

    Background Before Java8 features, if we had do operations like sum, multiplication, for example consider summation of…

    3 条评论
  • var in Java : variable with no type ?

    var in Java : variable with no type ?

    var was introduced in Java 10 Before jumping into the concept, lets glance through few terms which might help in…

社区洞察

其他会员也浏览了