1.interfaces and Lambda Expressions in Java
1.interfaces and Lambda Expressions in Java
1.interfaces and Lambda Expressions in Java
static methods: Method with body and qualified with static keyword
interface A{
void print();
}
interface B{
void print();
}
class C implements A, B
{
public void print()
{
System.out.println("Hello");
}
public static void main(String args[])
{
C obj = new C();
obj.print();
}
}
Output:
Hello
Multiple inheritance when default method with same
signatures is available in two interfaces
interface A class C implements A,B
{
{ public void show()
default void show() {
{ System.out.println("New definition of show");
}
System.out.println("A"); }
} public class Main
{
}
public static void main(String[] args) {
interface B A obj1=new C();
{ obj1.show();
B obj2=new C();
default void show()
obj2.show();
{ }
System.out.println("B"); }
Output:
} New definition of show
} New definition of show
Key point from the last program
If class C is not overriding the show() method, then error would
arise, so it becomes mandatory for the class to override the
method which implements two interfaces which further contain
default methods of same signatures.
Abstract class vs Interface
• Type of methods: Interface can have only abstract methods. Abstract
class can have abstract and non-abstract methods. From Java 8, it can
have default and static methods also.
• Final Variables: Variables declared in a Java interface are by default final.
An abstract class may contain non-final variables.
• Type of variables: Abstract class can have final, non-final, static and non-
static variables. Interface has only static and final variables.
• Implementation: Abstract class can provide the implementation of
interface. Interface can’t provide the implementation of abstract class.
• Inheritance vs Abstraction: A Java interface can be implemented using
keyword “implements” and abstract class can be extended using keyword
“extends”.
• Multiple implementation: An interface can extend another Java interface
only, an abstract class can extend another Java class and implement
multiple Java interfaces.
• Accessibility of Data Members: Members of a Java interface are public by
default. A Java abstract class can have class members like private,
protected, etc.
Default Method in Interface
Since Java 8, we can have method body in interface. But we need to make it default method.
Let's see an example:
interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceDefault{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}
}
Static Method in Interface
Since Java 8, we can have static method in interface. Let's see an example:
interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}
class TestInterfaceStatic{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}
Functional Interfaces In Java
• 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.
• Aside from allowing multiple statements, block lambdas are used much like
the expression lambdas just discussed. One key difference, however, is that
you must explicitly use a return statement to return a value. This is
necessary because a block lambda body does not represent a single
expression.
Example
Using Lambda:
(int a, int b) -> return(a+b);
OR
(a, b) -> return(a+b);
Structure of a lambda expression
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);}
interface A {
void show();
}
public class Main implements A {
void show()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A ref=new Main();
ref.show();
}
}
A. Hello
B. Blank output
C. Compile time error
D. Runtime error
Q4(Output??)
interface A {
void show();
}
interface B {
void show();
}
public class Main implements A,B {
public void show()
{
System.out.println("Hello");
}
public static void main(String args[])
{
Main ref=new Main();
ref.show();
}
}
A. Hello
B. Blank output
C. Compile time error
D. Runtime error
Q5(Output??)
interface A {
void show();
void display(){ System.out.println("Welcome");}
}
public class Main implements A {
public void show()
{
System.out.println("Hello");
}
public static void main(String args[])
{
A ref=new Main();
ref.display();
}
}
A. Compile time error
B. Hello
C. Welcome
D. Runtime error
Q6(Output??)
interface A {
protected int x=12;
public void show();
}
public class Main implements A {
public void show()
{
System.out.println(x);
}
public static void main(String args[])
{
A ref=new Main();
ref.show();
}
}
A. 12
B. 0
C. Compile time error
D. Runtime error
Q7(Output??)
interface A {
int x=12;
}
interface B {
int y=13;
}
interface C extends A,B
{
static int sum(){ return x+y;}
}
public class Main implements C {
public static void main(String args[])
{
System.out.println(C.sum());
}
}
A. 25
B. Compile time error
C. Runtime error
D. Blank output
Q8(Output??)
interface A
{
int x=10;
}
class B
{
static int y=13;
}
public class Main extends B implements A {
public static void main(String args[])
{
System.out.println(x+B.y);
}
}
A. Compile time error
B. 10
C. 23
D. Runtime error
Q9(Output??)