DEV Community

vikash-agrawal
vikash-agrawal

Posted on

Java Updates

Java 8 updates

Functional Interface

• An interface with just 1 abstract method is called Functional Interface.
• A Functional Interface only can be used for lambda as there is no way of combining more than 1 method to the single instance.

Lambda

• It’s a way to override a method in a simpler way.
• It can be single-statement without any curly braces.
• It can be multiline enclosed in the curly braces.
• Single statement outcome will be “return” value (No need of explicit return value)
• Content of lambda can be extracted to a private method.
• A lambda method can be with parameter or without parameters (in case of single parameter).

Default Method

• Java 8 onwards allows to have method definition with keyword default in the interface.
• 2 interfaces with same default method mandate the method to be overridden in the class implementing these 2 interfaces. (I’s the same reason that java doesn’t allow 2 classes to be extended.)

Static method

• Similar to static method in the given class, interface also allows to have the static method.
• The static method in interface is the property of the interface. Means this method can be accessed using the interface only not by another interface extending this interface or class implementing this interface.

Foreach method

• Iterable interface, a super interface of collection framework/interface, have the default method implementation of foreach method.
• To iterate through the element in the collection framework, the traditional way of doing it is getting the iterator and iterate through each of the elements and then perform the business logic inside the for loop.
• Foreach method will have the iteration logic within it, that means the client just needs to take care of business logic.
• Other difference between this approach is, foreach method will use the internal iterator as compared to external iterator in traditional logic. And this external iterator has to be taken care with the ConcurrentModificationException.

Top comments (0)