Professional Documents
Culture Documents
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
Computer Keyboard
Provide: Function keys,
Symbols, Alphanumeric,
Modifier keys, etc…
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-
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.
interface Sort {
<<Search>> <<Sort>> void sort (int array[], int n);
}
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
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
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}
Nested interface
Applying interfaces