Friday 20 November 2020

Abstract class v/s Interface in Java 8

 

Abstract class v/s Interface in Java 8

  • At a very high level, it looks very similar but actually they are different in many ways.
  • Also, considering the fact that default method in interface helps us to achieve loose coupling and backward compatibility
 
No.
 
 
Abstract Classes
 
Interface
1Contains members variablesAll variables are actually constants
2It can have constructorsInterface cannot have constructors
3Can hold state of an object using instance member variablesSince, all variables are static and final therefore no concept of holding state of an object
4Forces to implement abstract methods or else declare class as abstractdefault methods can be overridden, if required but never forces

Java 8 – Functional Interface

 

What is Functional Interface ?

  • An interface which contains only one abstract method is called as Functional interface
  • In short, it is called as Single Abstract Method i.e.; SAM
  • In addition to one abstract method, we can have any number of static or default methods, another feature added in Java 1.8 version
  • An optional annotation @FunctionalInterface can be annotated for Functional Interface to make sure that not more than one abstract method is declared
  • If more than one abstract method declared and also annotated with @FunctionalInterface, then compiler throws error
  • Note: To invoke Lambda Expression we need Functional Interface and it is required

To multiply any two Integer

@FunctionalInterface
interface DemoInterface {
    public int multiply(int i, int j);
}
 
public class TestLambdaExpression {
 
    // main method
    public static void main(String[] args) {
 
        // Lambda Expression
        DemoInterface d = (i, j) -> i*j;
 
        // how to invoke Lambda Expression -> Functional Interface
        System.out.println(d.multiply(7, 21));
    }
}

Output:

147

Saturday 14 November 2020

Java 8 – Lambda Expression


Lambda Expression in Java 1.8 version

It is introduced in Java ecosystem to bring benefits of Functional programming concepts. Let us look at attributes about it,

  • It is an anonymous function or nameless i.e.; a method without name
  • Return type is not required, as it could be inferred during course of program execution
  • Absolutely not required to mention access-modifiers, as it will be local to where it is declared/defined. But declaring doesn’t result into any error.

So what is actually required to identify a Lambda expression,

  • arguments lists, if any otherwise empty parenthesis ( )
  • execution part or logic or body or set of correct java statement

Lambda expression syntax

(argument list)->{function_body};

Where,

  • argument lists is number of input arguments or parameter
  • function body (method implementation) is set of Java statement within curly braces
  • And there is special symbol for Lambda expression that is hyphen followed by greater than sign i.e.; ->
    Example – (argument_list) -> {method_body};
  • There are certain rules regarding above syntax (->) which we will discuss at the end of this topic


Lambda Expression consists following things only,

  • input arguments list inside parenthesis
  • method body within curly braces

@FunctionalInterface
interface DemoInterface {
    public void display();
}
 
public class TestLambdaExpression {
 
    // main method
    public static void main(String[] args) {
 
        // Lambda Expression equivalent for above method
        DemoInterface d = () -> System.out.println("Hello World");
 
        // how to invoke Lambda Expression -> Functional Interface
        d.display();
    }
}