Lambda expression are essentially of type of functional interface. Simplest answer is to enable the functionality of lambda expression in java. This is a good candidate for your next interview question. Why default methods were needed in java 8? Simply add a default method in interface which they all implement. They provide you an highly desired capability of adding a capability to number of classes without even touching their code.This enables you to define methods out of your class and yet share with all child classes. This makes it easier for you to organize helper methods in your libraries you can keep static methods specific to an interface in the same interface rather than in a separate class. Static default methods: You can define static default methods in interface which will be available to all instances of class which implement this interface.Now it’s own custom method will be called. It can directly call instance.move() Īnd if class willingly wants to customize the behavior then it can provide it’s own custom implementation and override the method. If any class implements this interface then it need not to implement it’s own version of move() method. Moveable interface defines a method move() and provided a default implementation as well. If you do not override them, they are the methods which will be invoked by caller classes. Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.Īs name implies, default methods in java 8 are simply default.
0 Comments
Leave a Reply. |