Professional Documents
Culture Documents
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
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.
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
@Functional Interface
A new annotation @FunctionalInterface has been introduced to avoid accidental addition of abstract
methods in the functional interfaces.
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.
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.
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.
which the lambda expression(anonymous function) passed as parameter above is matched against.
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.
@author Manish
In other words, for each item in the collection you create a new object based on that item.
This example maps all strings in the items collection to their uppercase equivalents.
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
This example
creates a stream,
i.e. all strings from the items collection which starts with the character o.