Module 2_4
Module 2_4
Interface in Java
▷ An interface in Java is a blueprint of ▷ Java Interface also represents
a class. It has static constants and the IS-A relationship.
abstract methods.
▷ The interface in Java is a mechanism
▷ It cannot be instantiated just
to achieve abstraction. There can be
only abstract methods in the Java like the abstract class.
interface, not method body.
▷ It is used to achieve abstraction and ▷ Writing an interface is similar
multiple inheritance in Java. to writing a class. But a class
▷ In other words, interfaces can have describes the attributes and
abstract methods and variables. It behaviors of an object. And
cannot have a method body. an interface contains
behaviors that a class
implements.
2
Why use Java interface?
There are mainly three
reasons to use interface.
They are given below.
▷ It is used to achieve
abstraction.
▷ By interface, we can
support the
functionality of multiple
inheritance.
▷ It can be used to
achieve loose coupling.
3
Interface and Class
SIMILARITY DIFFERENCE
5
Java Interface Example
interface printable { ▷ When a class implements an
void print(); interface, you can think of the
class as signing a contract,
} agreeing to perform the
class A6 implements printable { specific behaviors of the
interface.
public void print()
▷ If a class does not perform all
{ System.out.println("Hello"); the behaviors of the interface,
} the class must declare itself as
abstract.
public static void main(String
args[]) {
▷ A class uses the implements
A6 obj = new A6(); Output: keyword to implement an
obj.print(); Hello interface.
}
} 6
Java Interface Example:
Bank
interface Bank{
float rateOfInterest();
▷ Output:
} ▷ ROI: 9.15
class SBI implements Bank{
public float rateOfInterest() { return
9.15f; }
}
class PNB implements Bank{
public float rateOfInterest() { return
9.7f; }
}
class TestInterface2{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI:
"+b.rateOfInterest());
}} 7
Multiple inheritance in Java by
interface
▷ If a class implements multiple interfaces, or an
interface extends multiple interfaces, it is known as
multiple inheritance.
8
Example
interface Printable{ public static void
void print(); main(String args[])
} {
interface Showable{
void show(); A7 obj = new A7();
} obj.print();
class A7 implements
obj.show();
Printable,Showable
{ }
public void print() }
{
System.out.println("Hello"); ▷ Output:
}
public void show() { ▷ Hello Welcome
System.out.println("Welcome");
}
9
Interface inheritance
interface Printable
{ public static void main(String
void print(); args[])
} {
interface Showable extends
TestInterface4 obj = new
Printable{
TestInterface4();
void show();
} obj.print();
obj.show();
class TestInterface4 implements
}
Showable
{ }
public void print()
{
System.out.println("Hello"); Output:
}
public void show() Hello
{
System.out.println("Welcome");
}
Welcome 10
Java 8 Default Method in Interface
▷ Since Java 8, we can have class TestInterfaceDefault
method body in interface. But
we need to make it default {
method. public static void main(String args[])
{
interface Drawable{
Drawable d=new Rectangle();
void draw();
default void msg() d.draw();
{ d.msg();
System.out.println("default
}
method");
} }
}
class Rectangle implements
Output:
Drawable{ drawing rectangle
public void draw()
{ default method
System.out.println("drawing
rectangle"); 11
}
Java 8 Static Method in
Interface
▷ Since Java 8, we can class TestInterfaceStatic
{
have static method in
public static void main(String args[])
interface
{
Drawable d=new Rectangle();
interface Drawable{
void draw(); d.draw();
static int cube(int x) System.out.println(Drawable.cube(3));
{
}
return x*x*x;
} }
}
class Rectangle implements
Output:
Drawable drawing rectangle
{
public void draw() 27
{
System.out.println("drawing
rectangle"); 12
Tagging Interfaces
▷ An interface with no methods in
it is referred to as a tagging
interface.
▷ Example:
package java.util;
13
Nested Interface in Java
▷ An interface can have ▷ Nested interface must be public if it is
another interface which is declared inside the interface but it can
known as a nested interface. have any access modifier if declared
▷ The nested interfaces are within the class.
used to group related ▷ Nested interfaces are declared static
interfaces so that they can implicitly.
be easy to maintain.
▷ The nested interface must be
referred by the outer
interface or class.
▷ It can't be accessed directly.
14
SYNTAX
▷ Interface with in ▷ Interface with in class
interface
interface interfacename class classname
{ {
... ...
interface interface
nestedinterfacename nestedinterfacename
{ {
... ...
} }
} } 15
Example of nested interface which is
declared within the interface
interface Showable{
void show(); public static void main(String args[])
interface Message{ {
void msg();
Showable.Message message =new
}
TestNestedInterface1(); //upcasting here
}
class TestNestedInterface1 message.msg();
implements Showable.Message { }
public void msg()
}
{
System.out.println("Hello nested
interface");
} Output:
}
hello nested interface
16
Example of nested interface
which is declared within the
class
public static void main(String args[]){
class A{
interface Message{ A.Message message=new
void msg(); TestNestedInterface2();//upcasting here
} message.msg();
} }
class TestNestedInterface2 }
implements A.Message
{
public void msg() Output:
{
System.out.println("Hello nested hello nested interface
interface");
}
17
Example : Interface and
class
20
Abstract class Interface
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since Java
abstract methods. 8, it can have default and static methods also.
7) An abstract class can be extended using keyword An interface can be implemented using keyword
"extends". "implements".
8) A Java abstract class can have class members like Members of a Java interface are public by default.
private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }
21
Example of abstract class and interface in
Java
//Creating interface that has 4 //Creating subclass of abstract class, now
methods we need to provide the implementation of
interface A{ rest of the methods
void a(); //bydefault, public and class M extends B{
abstract public void a(){System.out.println("I am
void b(); a");}
void c(); public void b(){System.out.println("I am
void d(); b");}
} public void d(){System.out.println("I am
d");}
//Creating abstract class that }
provides the implementation of
one method of A interface //Creating a test class that calls the
abstract class B implements A{ methods of A interface
Output:
public void c() class Test5{
{ public static void main(String
I am a args[]){
System.out.println("I am C"); A a=new M(); I am b
} a.a(); I am c
} a.b(); I am d
a.c();
22
a.d();
Example
interface Arithmetic
public void comparison()
{
{
void operations();
if (n1>n2)
}
System.out.println("n1 is grerater than n2");
interface Relational
else if (n1<n2)
{
System.out.println("n1 is less than n2");
void comparison();
else
System.out.println("n1 is equal n2");
}
class sample implements Arithmetic,
}
Relational
}
{
int n1,n2;
class Main{
sample(int a,int b)
public static void main(String args[])
{
{
n1=a;
sample s=new sample( 34,56);
n2=b;
s.operations();
}
s.comparison(); Addition :90
public void operations()
} subtraction:-22
{
} multiplication :1904
System.out.println( "Addition :"+
(n1+n2)); Division :0.60714287
System.out.println( "subtraction :"+ n1 is less than n2
(n1-n2)); 23
System.out.println( "multiplication :"+
THANKS
24