Static and Default methods in Interfaces in Java
Default Methods
The reason we have default methods in interfaces is to allow the developers to add new methods to the interfaces without changing the code in all the classes that implements these interfaces.
The method message() in Phone is a default method, which means we need not implement this method in the implementation class AndriodPhone. This way we can add the default methods to existing interfaces without bothering about the classes that implements these interfaces.
Static Methods
As mentioned above, the static methods in the interface are similar to the default method so we need not to implement them in the implementation classes. We can safely add them to the existing interfaces without changing the code in the implementation classes. Since these methods are static, we cannot override them in the implementation classes.
For-each Loop
The Java for-each loop or enhanced for loop is introduced since J2SE 5.0. It provides an alternative approach to traverse the array or collection in Java. It is mainly used to traverse the array or collection elements. The advantage of the for-each loop is that it eliminates the possibility of bugs and makes the code more readable. It is known as the for-each loop because it traverses each element one by one.
The drawback of the enhanced for loop is that it cannot traverse the elements in reverse order. Here, you do not have the option to skip any element because it does not work on an index basis. Moreover, you cannot traverse the odd or even elements only.
But, it is recommended to use Java for-each loop for traversing the elements of array and collection because it makes the code readable
Functional Interface and Lambda Expression
A functional interface in Java is an interface that contains only a single abstract (unimplemented) method. A functional interface can contain default and static methods which do have an implementation, in addition to the single unimplemented method.
Because it just has one method and that method has no implementation, the above counts as a functional interface in Java. Normally, a Java interface does not include implementations of the methods it declares, although default methods and static methods can include implementations. Another Java functional interface with implementations of some of the functions is shown below:

A Java functional interface can be implemented by a Java Lambda Expression. A Java lambda expression implements a single method from a Java interface. In order to know what method the lambda expression implements, the interface can only contain a single unimplemented method. In other words, the interface must be a Java functional interface.
Date class has even become obsolete. The new classes intended to replace Date class are LocalDate, LocalTime LocalDateTime and Instant.
- The LocalDate class represents a date. There is no information of a time or time zone.
- The LocalTime class represents a time. There is no information of the date or time zone.
- The LocalDateTime class represents a date and time. There is no information of a time zone.
- The Instant class represents the timestamp at any moment.
JAVA Stream API
Stream does not store elements. It simply conveys elements from a source such as a data structure, an array, or an I/O channel, through a pipeline of computational operations.
0 Comments