You are on page 1of 20

Interfaces

G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Textbook
 Click to edit Master text styles
 Second level
 Third level
◦ Fourth level
◦ Fifth level

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 2


Overview of topics
 What is an Interface?
 Defining an interface
 Implementing interface
 Access method implementation through Interface reference
 Partial implementation of interface
 Nested interface
 Applying interfaces
 Inheriting interfaces / Extending interfaces
 Default interface method
 Static method in interface

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 3


Interfaces  An abstract method specifies the interface to a method but
provides no implementation. Subclass must provide its own
What is an Interface? implementation of each abstract method defined by its
Defining an interface superclass.
Implementing interface  Separation of class interface from its implementation is
Access method implementation
done using interfaces.
through Interface reference
 In an interface, no method (except default method) can
Partial implementation of interface include a body
Nested interface
 Once an interface is defined, any number of classes can
Applying interfaces implement it and give its own definition.
Inheriting interfaces / Extending  One class can implement any number of interfaces.
interfaces

Default interface method  To implement an interface, the implementing class must


provide a body to the methods provided by the interface.
Static method in interface

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 4


What is an Interface?
 Consider a Keyboard Interface & its implementation

Computer Keyboard
Provide: Function keys,
Symbols, Alphanumeric,
Modifier keys, etc…

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 5


Interface
Mobile Keyboard
Provide: Function keys,
Symbols, Alphanumeric,
Modifier keys, etc…

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 6


 Syntax
Interfaces Interface:
What is an Interface? access interface InterfaceName
{
Defining an interface
type final-var1 = value;
Implementing interface type final-var2 = value;
Access method implementation
// ...
through Interface reference type final-varN = value;
Partial implementation of interface
ret-type method-name1(param-list);
Nested interface ret-type method-name2(param-list);
Applying interfaces // ...
ret-type method-nameN (param-list);
Inheriting interfaces / Extending }
interfaces

Default interface method


Implementing Class:
Static method in interface access class ClassName
[extends superclassname]
[implements interface0 [,interface1[,...]] {
// class-body
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 7
Interface:
access interface InterfaceName

Defining an interface
{
type final-var1 = value;
type final-var2 = value;
// ...
type final-varN = value;
 access is either public or default. ret-type method-
name1(param-list);
 When declared as public name2(param-list);
ret-type method-

 interface can be used by any other code. // ...


ret-type method-nameN
(param-list);
 it must be in a file of the same name. }

Implementing Class:
 Interface name can be any valid identifier access class ClassName
[extends superclassname]
[implements interface0
 Methods declared are abstract methods [,interface1[,...]] {
// class-body
}
 Variables declared in an interface are not instance variables, but are public, final, and static and
must be initialized. They are essentially constants.

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 8


 Example - 1
Interfaces interface Shape {
<<Shape>>
double PI = 3.14159;
What is an Interface?
double area();
Defining an interface
} Circle Rectangle
Implementing interface

Access method implementation class Circle implements Shape {


through Interface reference private double radius;
Partial implementation of interface public double area() {
Nested interface return PI * radius * radius;
}
Applying interfaces
}
Inheriting interfaces / Extending
interfaces
class Rectangle implements Shape {
Default interface method
private double breadth, height;
Static method in interface public double area() {
return breadth * height;
}
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 9
interface Search {
 Example - 2 void search (int array[], int n, int key);
}

interface Sort {
<<Search>> <<Sort>> void sort (int array[], int n);
}

class LinearSearch implements Search {


public void search (int array[], int n, int key) {
LinearSearch BinarySearch //Linear Search Implementation
}
}

class BinarySearch implements Search, Sort { // Multiple Inheritance


public void search (int array[], int n) {
sort (array, n);
//Binary Search Algorithm
}
public void sort (int array[], int n) {
//Quick Sort Algorithm
}
Interfaces } GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 10
public interface Direction { //Declaring constants through interface
int TOP = 0;
int RIGHT = 1;
int BOTTOM = 2;
 Example – 3: Constants int LEFT = 3;
}

class A implements Direction{


public void meth1(int direction) {
if(direction == TOP) // Using inherited interface constants
System.out.println(“TOP”);
else if(direction == BOTTOM) // Using inherited interface constants
System.out.println(“BOTTOM”);

}
}

class B {
public void meth1(int direction) {
if(direction == Direction.TOP) // Using interface constants
System.out.println(“TOP”);
else if(direction == Direction. BOOTTOM) // Using interface constants
System.out.println(“BOTTOM”);

}
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 11
Interfaces  Reference variable of an interface Shape {
double PI = 3.14159;
interface can be used to
What is an Interface? hold the instance of its double area();
Defining an interface implementing class }
Implementing interface
class Circle implements Shape {
Access method implementation
through Interface reference private double radius;
public Circle(double r) { radius = r; }
Partial implementation of interface
public double area() {
Nested interface return PI * radius * radius;
Applying interfaces }
Inheriting interfaces / Extending }
interfaces

Default interface method class Demo {


Static method in interface
public static void main(String a[]) {
Shape c = new Circle(5);
System.out.println(“Area=“ + c.area());
}
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 12
Interfaces  If a class includes an interface but does not fully implement
the methods required by that interface, then that class
What is an Interface? must be declared as abstract.
Defining an interface
 Ex.: The Box class does not implement area() method.
Implementing interface Hence Box becomes an abstract class.
Access method implementation
through Interface reference interface Shape {
Partial implementation of interface double PI = 3.14159;
Nested interface
double area();
}
Applying interfaces

Inheriting interfaces / Extending abstract class Box implements Shape {


interfaces
private double length, breadth;
Default interface method

Static method in interface public double perimeter() {
return length * breadth;
}
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 13
Interfaces  An interface can be declared a member of a class or
another interface. Such an interface is called a member
What is an Interface? interface or a nested interface.
Defining an interface
 A nested interface can be declared as public, private, or
Implementing interface protected.
Access method implementation  This differs from a top-level interface, which must either be
through Interface reference
declared as public or use the default access level,
Partial implementation of interface
 When a nested interface is used outside of its enclosing
Nested interface
scope, it must be qualified by the name of the class or
Applying interfaces interface of which it is a member.
Inheriting interfaces / Extending
interfaces

Default interface method

Static method in interface

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 14


Nested interface
 Click to editclass A { text styles
Master
public interface NestedIF {
 Second level boolean isNotNegative(int x);
 Third level }
}
◦ Fourth level
◦ Fifthclass
level
B implements A.NestedIF {
public boolean isNotNegative(int x)
{ return x < 0 ? False : true; }
}

class NestedIFDemo{
public static void main(String argv[])
{
A.NestedIF nif = new B();

if(nif.isNotNegative(10))
System.out.println(“10 is ! Negative”);
if(nif.isNotNegative(-12))
System.out.println(“this wont be displayed”);
}
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 15
// Define an integer stack interface.
interface IntStack {
void push(int item); // store an item
Interfaces }
int pop(); // retrieve an item

What is an Interface? // An implementation of IntStack that uses fixed storage.


class FixedStack implements IntStack {
Defining an interface
private int stck[];
Implementing interface private int tos;

Access method implementation FixedStack(int size) { // allocate and initialize stack


through Interface reference stck = new int[size];
tos = -1;
Partial implementation of interface }
Nested interface public void push(int item) { // Push an item onto the stack
Applying interfaces if(tos==stck.length-1) // use length member
System.out.println("Stack is full.");
Inheriting interfaces / Extending else
interfaces stck[++tos] = item;
}
Default interface method public int pop() { // Pop an item from the stack
if(tos < 0) {
Static method in interface System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
Interfaces } GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 16
Applying interfaces
 Click to edit Master text
class IFTest { styles
public static void main(String args[]) {
 Second level
 Third level FixedStack mystack1 = new FixedStack(5);
FixedStack mystack2 = new FixedStack(8);
◦ Fourth level
// push some numbers onto the stack
◦ Fifth level for(int i=0; i<5; i++)
mystack1.push(i);
for(int i=0; i<8; i++)
mystack2.push(i);

// pop those numbers off the stack


System.out.println("Stack in mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());

System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 17


Interfaces  One interface can inherit another by use of the keyword
What is an Interface? extends.
Defining an interface  The syntax is the same as for inheriting classes.
Implementing interface
 When a class implements an interface that inherits another
Access method implementation interface, it must provide implementations for all methods
through Interface reference
required by the interface inheritance chain.
Partial implementation of interface

Nested interface

Applying interfaces

Inheriting interfaces / Extending


interfaces

Default interface method

Static method in interface

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 18


Inheriting interfaces / // This class must implement all of A and B

Extending interfaces class MyClass implements B {

public void meth1() {


<<A>>
 Click to edit Master text styles System.out.println("Implement meth1().");
meth1(): void }
 Second level meth2(): void
 Third level public void meth2() {
◦ Fourth level System.out.println("Implement meth2().");
<<B>> }
◦ Fifth level meth3(): void
public void meth3() {
System.out.println("Implement meth3().");
MyClass }
}
// One interface can extend another.
interface A { class IFExtend {
void meth1(); public static void main(String arg[]) {
void meth2(); MyClass ob = new MyClass();
} ob.meth1();
ob.meth2();
// B now includes meth1() and meth2() -- it adds meth3(). ob.meth3();
interface B extends A { }
void meth3(); }
}
Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 19
End of Interfaces

Interfaces GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 24

You might also like