0% found this document useful (0 votes)
33 views14 pages

Chapter 5 Interface

Uploaded by

tiashamandal91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views14 pages

Chapter 5 Interface

Uploaded by

tiashamandal91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Interface

BY
P RAT I M A S A R KA R
CSE TINT
Interface
Using the keyword interface, you can fully abstract a class’ interface from its implementation.

That is, using interface, you can specify what a class must do, but not how it does it.

 Interfaces are syntactically similar to classes, but they lack instance variables, and their methods
are declared without any body.
In interface variables are final and static.
In interface methods are public and abstract.
Once it is defined, any number of classes can implement an interface. Also, one class can
implement any number of interfaces (multiple inheritance).
Interface
To implement an interface, a class must create the complete set of methods declared by the
interface.
 However, each class is free to determine the details of its own implementation.
By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple
methods” aspect of polymorphism.
Interfaces are designed to support dynamic method resolution at run time.
Why use Java interface?
here 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 runtime polymorphism.
How to declare an interface?
access interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
return-type method-nameN(parameter-list);
type final-varnameN = value;

}
Internal addition by the
compiler
The relationship between classes
and interfaces
Example
interface printable{
void print();
}
class A6 implements printable{
public void print(){System.out.println("Hello");}

public static void main(String args[]){


A6 obj = new A6();
obj.print();
}
}
Hierarchical Inheritance in
interface
//Interface declaration: by first user //Using interface: by third user
interface Drawable{
class TestInterface1{
void draw();
public static void main(String args[]){
}
//Implementation: by second user Drawable d=new Circle();//
In real scenario, object is provided by method e
class Rectangle implements Drawable{
.g. getDrawable()
public void draw()
{System.out.println("drawing rectangle");} d.draw();
} }
class Circle implements Drawable{
}
public void draw(){System.out.println("drawing circle");}
}
Multiple inheritance in Java by
interface
Multiple inheritance in Java by
interface
interface Printable{ Class TestInterface3 {
void print(); public static void main(String args[]){
} Test obj = new Test();
interface Showable{ obj.print();
void print();
}
}
}
Class Test implements Printable, Showable{
public void print(){
System.out.println("Hello");}
}
Interface inheritance
interface Printable{ public static void main(String args[]){
void print(); TestInterface4 obj = new TestInterface4();
} obj.print();
interface Showable extends Printable{
obj.show();
void show();
}
}
}
class TestInterface4 implements Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
Dynamic method dispatch(Runtime polymorphism) by interface
Interface S{ class demo
void callme(); {
} public static void main(String args[])

class A implements S{ {
public void callme(){ S s1; //reference of interface S
S.o.pln (“inside A”); }} A a1=new A();
B b1=new B();
class B implements S{
public void callme() s1=a1;
{ s1.callme()// calls A’s version
S.o.pln (“inside B”); s1=b1;
}}
s1.callme() // calls B’s version
}}
Thank You

You might also like