Beruflich Dokumente
Kultur Dokumente
By
Ravi Kant Sahu
Asst. Professor, LPU
Contents
Interface
Functional Interface
Introduction of Lambda Expressions
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Interface
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Defining Interface
public interface My
{
int x = 10; // by default public static and final
void demo();
// by default public and abstract
default void show() {…}
// default method, public
static void test(){…}
// static method, public
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Interface Methods
We can have three types of methods in an interface:
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Default Methods
Default Method allows to add new methods to
the interfaces without breaking the existing implementation
of an interface.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Implementing Interfaces
When a class implements an interface, then it has to perform
the specific behaviors of the interface.
If a class does not perform all the behaviors of the interface, the
class must declare itself as abstract.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
interface Animal
{
void eat();
public void travel();
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
public class Mammal implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public int noOfLegs()
{ return 0; }
public static void main(String args[])
{
Mammal m = new Mammal();
m.eat();
m.travel();
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Abstract Class Vs Interfaces
Abstract Class Interface
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Functional Interface
A functional interface is an interface that specifies only
one abstract method.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example:
@FunctionalInterface
interface MyInterface
{
int test(int n);
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example:
@FunctionalInterface
interface MyInterface
{
int test(int n);
public boolean equals(Object ob);
public String toString();
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Lambda Expression
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Fundamentals of Lambda Expressions
Lambda Expression introduces a new operator (−>) which is
referred to as the lambda operator or the arrow operator.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
Using Lambda:
(int a, int b) -> return(a+b);
OR
(a, b) -> return(a+b);
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Important
When a lambda expression has only one parameter, it is
not necessary to surround the parameter name with
parentheses when it is specified on the left side of the
lambda operator.
Example:
boolean isEven(int n){
return (n%2==0);
}
Using Lambda Expression: n -> (n % 2)==0;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Structure of Lambda Expressions
Argument List:
A lambda expression can contain zero or more arguments.
// No argument
( ) -> { System.out.println("No argument"); }
// Single argument
(int arg) -> { System.out.println(“One argument : " + arg); }
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Structure of Lambda Expressions
Body of a lambda expression:
When the code on the right side of the lambda operator consists
of a block of code that can contain more than one statement, It is
called as block body or Block Lambda.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Characteristics of Lambda Expressions
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Using Lambda Expressions to create
Thread
class MyABCD extends Thread {
public void run() {
char c = 'A';
try{ for(int i=0; i<26; i++)
{
System.out.println(c++); Thread.sleep(500);
}
}
catch(Exception e){}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Using Lambda Expressions to create
Thread
class MyCount implements Runnable {
public void run()
{ try{ for(int i=1; i<11; i++)
{
System.out.println(i);
Thread.sleep(500);
}
}
catch(Exception e){}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Using Lambda Expressions to create
Thread
class ThreadDemo{
public static void main(String [] rk) {
Thread t1 = new Thread(new MyABCD()); t1.start();
Thread t2 = new Thread(new MyCount()); t2.start();
Thread t3 = new Thread(
()->{ try{
for(int i=1; i<11; i++){
System.out.println("Threads are running");
Thread.sleep(1000); }
} catch(Exception e){}
} );
t3.start(); }
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)