4 Interface
4 Interface
interface Callback{
void meth(int param);
}
Implementing Interface
One or more classes can implement the interface.
The general form of a class that includes the
“implements”:
access class classname [extends superclass]
[implements interface_name [,interface_name....]]
{
//class body
}
access is either public or not used.
If a class implements two interface the interfaces is
separated by comma.
The methods that implement an interface must be
declared public.
The type signature of the implementing method
must exactly the the type signature specified in the
interface definition.
Example:
class Client implements Callback {
// Implement Callback's interface
public void meth(int p) {
System.out.println("callback called with " + p);
}
}
The class that implements interface have also
some own methods.
Example:
class Client implements Callback {
// Implement Callback's interface
public void meth(int p) {
System.out.println("callback called with " + p);
}
void nonIfaceMeth() {
System.out.println("Classes that implement interfaces " +
"may also define other members, too.");
}
}
Accessing through interface
Reference
class AnotherClient implements Callback {
// Implement Callback's interface callback called with 42
public void meth(int p) { Another version of callback
System.out.println("Another version of
p squared is 1764
callback");
System.out.println("p squared is " + (p*p));
}
}
Class Box {…..}
c=c1;
class TestIface2 {
c.meth(42);
public static void main(String args[]) {
c. nonIfaceMeth();
Callback c; Can't write, because reference variable is of Client class (interface),
so it cannot access a method of Client class
c = ob; // c now refers to
Client c1=new Client(); AnotherClient object
AnotherClient ob = new AnotherClient(); c.meth(42);
}
}
Interface and Dynamic method
dispatch
Differences between abstract class
and interfaces
Feature Interface Abstract class
Multiple A class may implement A class may extend only one
inheritance several interfaces. abstract class.
interface IntMinMax{
int _min();
int _max();
}
Save: IntMinMax.java
IntClass1.java
class ArrayClass implements IntMinMax{
int a[]=new int[10];
ArrayClass(int b[]) public int _max()
{ {
a=b; int c,i;
} c=a[0];
public int _min()
{ for(i=1;i<a.length;i++)
int c,i; {
c=a[0];
for(i=1;i<a.length;i++) if(a[i]>c)
{
c=a[i];
if(a[i]<c)
}
c=a[i];
return c;
}
}
return c;
}
}
class IntClass1{
public static void main(String args[])
{
int arr[]={50,8,9,1,2,7,10,6};
ArrayClass ob1=new ArrayClass(arr);
System.out.println(ob1._min());
System.out.println(ob1._max());
1
} 50
}
IntClass2.java
class IntNum implements IntMinMax{ public int _max()
int a,b,c; {
IntNum(int i,int j,int k) if(a>b)
{ {
a=i; if(a>c) return a;
b=j; else return c;
c=k; }
} else
public int _min() {
{ if(b>c) return b;
else return c;
if(a<b)
}
{
}
if(a<c) return a;
}
else return c;
}
else
{
if(b<c) return b;
else return c;
}
}
class Intclass2{
public static void main(String args[])
{
IntNum ob1= new IntNum(3,10,5);
System.out.println(ob1._min());
System.out.println(ob1._max());
}
3
} 10
See example
◼ Page 243 (applying interfaces)
⚫ Stack Example
Variables in Interfaces
interface SharedConstants {
int Even = 0; Just like #define or const of
int Odd = 1; C/C++.
}
}
}
Interfaces Can Be Extended
// One interface an extend another.
interface A { class IFExtend {
void meth1(); public static void main(String arg[])
void meth2(); {
} MyClass ob = new MyClass();
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A { ob.meth1();
void meth3(); ob.meth2();
} ob.meth3();
}
// This class must implement all of A and B
}
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}
public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}