Advanced Java Flashcards
Java 8 features
What are the two new packages in Java 8?
java. util.function
java. util.stream
What is a Functional Interface?
The interface having only one abstract method is called functional interface. It represents abstract concepts such as functions, actions or predicates.
For eg. Predicate, Consumer, Function, Supplier
Explain the Predicate interface.
It has a method named test which takes one argument and evaluates it to return boolean value.
Explain the Consumer interface.
It accepts single argument with no return value.
What is an Anonymous class?
Anonymous class is an inner class with no name.
They are useful when we have to modify the implementation of a method only once of some class/interface on the fly without actually having to extend the class/implementing the interface.
For eg.
@FunctionalInterface
public interface Foo {
public int calc (int a, int b);
}
If we want to provide a different implementation for this method but do not want to implement the Foo interface, we can have an anonymous inner class like this:
Foo foo=new Foo ( ) { @Override public int calc( int a, int b) { return a+b; } };
And then call it like this:
System.out.println ( foo.calc ( 10, 20 ) );
If we want the function to perform the subtraction, we can modify the behaviour.
With anonymous inner class, we can avoid adding new .java files to project. This comes in handy during creation of various event listeners. If we do not use anonymous inner classes, a separate class that implements the event listener interface will be required for each event. This approach will take lot of coding for a simple operation.
Explain lambda expression.
If we are implementing a functional interface using an anonymous class, the code might seem lengthy to perform a single operation. With lambda expression, we can minimize the amount of code and treat the functionality as method argument.
For eg.
@FunctionalInterface
public interface Foo {
public int calc (int a, int b);
}
Implementation of this using anonymous inner class:
Foo foo=new Foo ( ) { @Override public int calc ( int a, int b) { return a+b; } };
Implementation using lambda expression:
Foo add = (a,b) -> a+b;
Foo diff = (a,b) -> a-b;
As this is a single expression (as opposed to block lambda), we can omit return statement.
Even when the method does not take any argument, lambda expression must specify the argument parenthesis.
How to create a Lambda expression without arguments?
Anonymous class:
Runnable r1 = new Runnable ( ) { @Override public void run ( ) { System.out.println ("run 1"); } };
Lambda expression:
Runnable r2= () -> System.out.println(“run 2”);
Explain method reference.
With lambda expression, we can take any method (static or instance) or even a constructor and convert it into a lambda. This is called method reference and is denoted with symbol ‘::’.
Consumer print = ( str ) -> System.out.println (str);
print.accept( “Hello” );
With method reference:
Consumer print =System.out : : println;
print.accept( “Hello” );
Explain the Comparator in Java 8.
We can use lambda expression to create a Comparato, such as
Comparator byAge =
(Player p1, Player p2) -> Integer.compare(p1.getAge( ), p2.getAge( ));
We can also use Comparator.comparing( ) method. It takes a property for comparing items and returns a Comparator instance:
Comparator byAge =
Comparator.comparing(Player :: getAge);
What are the features of Java 8?
- Functional Interfaces and Lambda expressions
- Interfaces with default and static methods
- Java Stream API
- Java Date Time API
What is a default method in interface?
Java 8 provides interfaces containing methods with body, labeled as default methods, also known as Defender methods or Virtual extension methods. This feature was introduced in Java 8 to provide backward compatibility so that existing interfaces can use the lambda expressions without getting broken.
If an interface is implemented by multiple classes, it becomes mandatory for all the classes to override all the methods in interface. With default methods, classes do not have to provide the behaviour.
For eg.
public default void logging ( ) {
System.out.println ( “IF1” );
}
If a class implements two interfaces containing exact same default methods, the class must provide its own implementation of the method to get rid of compile-time error.
Explain static methods in interface.
Apart from default methods, Java 8 also allows us to define and implement static methods in interface. They make it possible to group related utility methods, without having to create separate utility classes that are simply placeholders for static methods.
Since static methods don’t belong to an object, they will not be part of API of classes implementing the interface and they will be called by using the interface name preceding the method name. Due to this, static methods in interfaces cannot be overridden in implementing classes. Trying to override the static method in implementing class with @Override annotation will result in compile-time error.
This static method in an interface can be invoked within other static and default methods.
For eg. public static void message( ) { System.out.println("inside static method"); } public default void logging ( ) { System.out.println ( "IF1" ); message( ); }