Showing posts with label Abstract method Java 8. Show all posts
Showing posts with label Abstract method Java 8. Show all posts

Saturday, June 14, 2014

Java 8 : Frequently Asked Questions for Interviews - 6

How can a Lambda Expression be converted to Functional Interface? Example.
When a Lambda expression is executed 
  • an object of some class that implements the functional interface is created behind the scenes.
  • when the abstract method on this object is invoked, the body of lambda expression is executed.
PTR: The management of these classes and objects are totally dependent on implementation but its still very efficient than using traditional inner classes.
  • This conversion to interfaces is what makes Lambda Expressions so compelling as the syntax is short and simple, easier to read.
Example:
Array.sort(cars, (Car firstCar, Car secondCar) ->
        firstCar.getColor().compareTo(secondCar.getColor() 
    );

Can you Assign a lambda expression to a variable ( ofcourse of type Object) ?
No, Since Object is not a Functional Interface. It does not have any abstract method.

Can we declare function types such as (String, String) -> int, as used in other programming languages that support function literals?
No, CONVERSION TO FUNCTIONAL INTERFACE is the ONLY THING that one can do with Lambda Expressions in Java.

  • There are no function types added to Java language in Java 8.
  • There are only Lambda expressions that can be converted to functional interfaces.
  • This is natural for Java programmers as they know that certain interfaces e.g.. Comparator has a specific purpose. Its not just a method with given params and return type. So when you want to use Lamdba Expressions , its important to keep the purpose in perspective and have a specific functional interface for it.
For example: java.util.function has various generic functional interfaces. One such interface is BiFunction(T, U, R), describes functions with parameter types T and U and a return type R. We can use it as
BiFunction comp = (int first, int second) -> Integer.compare(first.length(), second.length())
However, that does not help with sorting, as Arrays.sort does not want a BiFunction. It wants a Comparator.

Are there any annotations introduced to help identify Functional Interfaces?
Yes, the annotation @FunctionalInterfaces can be used to tag any functional interface. It has two advantages 

  • The compiler will check the annotated entity to see if it has a single abstract interface or not.
  • The Javadoc page will include an statement that the annotated entity is a functional interfaces
However, the use of @FunctionalInterface is totally optional. Any interface having a single abstract method can be used a functional interface.

Does Checked exceptions matter in lambda Expressions? If yes, when and when not?
Yes, Checked exceptions matter when a lambda is converted to an instance of a functional interface. In case the body of lambda expression throws a checked exception, that exception needs to be declared in the abstract method of functional interface.
For example, Callable interface has an abstract call method that throws "Exception". However, the Run method in Runnable interface throws no exception of any sort.
Declaration :

public interface Callable<V> has abstract method V call throws Exception
Callable sleepDuringCall = () -> { 
    System.out.println("Calling"); 
    Thread.sleep(10000);} 
Thread.sleep (declared as public static void sleep(long miliseconds) throws InterruptedException) can throw checked exception.

Whereas,
public interface Runnable has abstract method void run()
Runnable sleepWhileRunning = () -> { 
    System.out.println("Sleeping during run"); 
    Thread.sleep(10); }
// will error out as Thread.sleep throws a checked exception.
However, you can catch the checked exception inside of the lambda body to keep the assignment legal.
Runnable sleepWhileRunning = () -> { 
    System.out.println("Sleeping during run"); 
    try{ 
        Thread.sleep(10); 
    }catch( InterrrupedException e){
        e.printStackTrace();
    }
}

Java 8 : Frequently Asked Questions for Interviews - 5


What are Functional Interfaces

A functional interface is an interface with a single abstract method. That abstract method is used as the type of a lambda expression.
public interface ActionListner extends EventListener {
      public void actionPerformed( ActionEvent event);
}
In the above example ActionListener has only one method actionPerformed and we use it to represent the action that takes one argument and returns no result. => It is a functional Interfaces.
  • It doesn't matter what the single method ( e.g. actionPerformed ) is called. 
  • The method will match up to lambda expression as long as it has compatible method signature
  • One can see the type of the parameter in the functional interface => improves readability and understanding in code.
  • Functional interfaces can use generics also.
  • Some important functional interfaces in Java :
Interface NameArgumentsReturnsExample
Predicate<T>sasdadasTdfasdfasdfafsadfsbooleanasfsdfsdadfsdHas this book been published yet?aasdfsdfasdfasdfasdfasdfasdfasfsdfsfsdfsdadfsd
Consumer<T>TvoidPrinting out a value. Getting an action/task done.
Function<T,R>TRGet a property/instance var from an object. Eg: get the author from book object
Supplier<T>NoneTAny Factory Method. Get Constant Values defined in certain context.
UnaryOperator<T>TTLogical not (!)
BinaryOperator<T> (T,T)(T,T)TMultiplying two numbers (*)

  • There are many interfaces in Java that encapsulates blocks of code such as "Runnable" or "Comparator"
  • Lambdas are backwards compatible with these interfaces.

When to supply Lambda Expression?

  • When an object of an interfaces with a single method is expected. Eg. ActionListerner is the functional interfaces in  the following example: The Left Hand Side Code changes to written in Lambda expression in the right hand side.
  • button.addActionListener( new ActionListener() {

        public void actionPerformed(ActionEvent event){
            System.out.println("Click on Button");
            }
    })
    button.addActionListerner( event ->
                 System.out.println("Click on Button"); )
  • The boiler plate code is minimized, readability enhanced and meaningfulness accomplished.

Why a functional interface must have a single abstract method. Aren’t all methods in an interface abstract?

  • In Java 8, interfaces can declare nonabstract methods.
  • Some interfaces in the Java API redeclare Objectmethods (such as toString or clone) to attach javadoc comments. But these declarations do not make the methods abstract.
    • Check out the Comparator API for an example.