You are on page 1of 5

forEach method implementation in Iterable interface is:

From Java 8, interfaces are enhanced to have method with implementation.

We can use default and static keyword to create interfaces with method implementation.

Default methods:

Problem : If we want to add additional methods (declaration) in the interfaces, it will require change
in all the implementing classes.

A maintenance nightmare !!

To resolve this issue, Java8 onwards we can add method with body in IF.
 This is called default method in java interface.
 We need to use “default” keyword with the method signature.
 Provide the default implementation (i.e. body).

Now when a class will implement Interface1, it is not mandatory to provide implementation for
default methods of interface.
While overriding, the visibility of default method must be public in implementing class.
See the examples at Java8.B_IF_Default.ImpDimondIssue

Default method leads to the Diamond Problem!!


Class1 implements if1, if2
Assume if1 and if2 has common default method then which definition to take up in class1?
In this case class1(implementing class) has to provide implementation for common default method
otherwise compiler will throw compile time error.
Note : This ambiguous case is same even if one interface has default method and other has
traditional interface method with same signature (of course without default key word).

See the examples at Java8.B_IF_Default.ImpDefaultIF

Since any class implementing an interface already has Object as superclass,

if we have equals(), hashCode() default methods in interface, it will become irrelevant.

That’s why for better clarity, interfaces are not allowed to have Object class default methods.
Static methods:

This is another type of methods with body (other than default) in IF.

It is very similar to static methods, like it is used with the reference of interface.

Interface2.print(str);

Important points:

 Java interface static method is part of interface; we can’t use it for implementing class
objects.
 Unlike classes (where Static methods are inherited), Static methods of interfaces are never
inherited.
Note, Class implements(not extends) interface
So, static method declared in IF doesn’t inherit to implementing class.
So we can’t use implementing class reference to access static method.
 Implementing classes can't override them like default method as it is static.
This helps us in providing security.

Practical use case:

We can use java interface static methods to remove utility classes such as Collections and move all of
it’s static methods to the corresponding interface, that would be easy to find and use.

Functional Interface

An interface with exactly one abstract method is known as Functional Interface. It may have

 one or more default IF


 one or more Static IF
 It will have exactly one abstract IF.

@Functional Interface

A new annotation @FunctionalInterface has been introduced to avoid accidental addition of abstract
methods in the functional interfaces.

It enables us to use lambda expressions

A new package java.util.function with bunch of functional interfaces are added to provide target
types for lambda expressions and method references.
lambda expressions

lambda expressions implements functionalIF and avoid bulky anonymous class implementation.

Since there is only one abstract function in the functional interfaces,

there is no confusion in applying the lambda expression to the method.

Compare the Lamda expression with anonymous function:


 Method body (business logic) remain same.
 Lambda Expressions syntax is (argument) -> (body).
 Body is for implemantation of abstract function defined in functional interface.

java Stream

http://tutorials.jenkov.com/java-collections/streams.html

A collection is an in-memory data structure to hold values and before we start using collection, all
the values should have been populated.

Whereas a java Stream is a data structure that is computed on-demand. Java Stream doesn’t store
data, it operates on the source data structure (collection and array) and produce pipelined data that
we can use and perform specific operations.

Such as we can create a stream from the list and filter it, based on a condition.

Once obtained, you use that stream to process the elements in the collection.

Processing the elements in the stream happens in two steps / phases:

 Configuration : This is first step.


The configuration can consist of filters and mappings.
 Processing : This is next step.
The processing consists of doing something to the filtered and mapped objects.

No processing takes place during the configuring calls. Not until a processing method is called on the
stream.
The filter() method takes a Predicate as parameter.

The Predicate interface contains a function called test()

which the lambda expression(anonymous function) passed as parameter above is matched against.

In other words, the lambda expression implements the Predicate.test() method.

The test() method is defined like this:

boolean test(T t)

When you call the filter() method on a Stream, the filter passed as parameter to the filter() method
is stored internally.

No filtering takes place yet.

If the Predicate.test() method

returns true for an item, that means it should be processed.

If false is returned, the item is not processed.

@author Manish

It is possible to map the items in a collection to other objects.

In other words, for each item in the collection you create a new object based on that item.

How the mapping is done is up to you.

This example maps all strings in the items collection to their uppercase equivalents.

Again, It only configures the stream for mapping.


Once one of the stream processing methods are invoked, the mapping (and filtering) will be
performed.

The collect() method is one of the stream processing methods on the Stream IF.

When this method is invoked, the filtering and mapping will take place and

the object resulting from those actions will be collected.

This example

creates a stream,

adds a filter, and

collects all object accepted by the filter in a List.

i.e. all strings from the items collection which starts with the character o.

You might also like