Java Exception and Interface

Download Report

Transcript Java Exception and Interface

Java Interface(สรุป)
from K.P. Chow
University of Hong Kong
(some slides from S.M. Yiu)
Interface
Users
A set of
interface
Data
(including implementation)
+
Operations
implementation
Interface
A collection of method definitions (without implementation)
Defines a set of methods without implementing them
IsEmpty( )
Users
Exists(x)
Add(x)
Interface
Declaration
Delete(x)
public interface Set_interface {
boolean IsEmpty();
Methods
boolean Exists(Object x);
Declaration
void Add(Object, x);
(public,
void Delete(Object, x);
abstract)
int nonsense = 0;
}
(public, static, final)
Set
Data:{a, b, c, …..}
Operation
Implementation
Details
Note: there is a
Set interface in Java
Interface
body
Constant
Declaration
A class “implements” an interface
Inherits the constants
Implement all methods declared in
the interface (& its superinterfaces)
public class example implements Set_interface {
……
Can implement
boolean IsEmpty() {…….};
> 1 interfaces
boolean Exists(Object x){…….};
void Add(Object, x){……..};
void Delete(Object, x){……..}; Implement all
}
these methods
Interface vs Abstract Class
One class can
implement >1
interfaces
A subclass can only
inherit from ONE
superclass
Conceptually, interface wants the classes
to guarantee a set of behavior (services)
while inheritance implies an “is-a”
relationship
Usages of Interfaces
• To simulate multiple inheritance without the problem of
name clash from different parents
• Specifies the exact signature of the methods that must be
provided by the implementation, e.g.
public interface DataInput {
int skipBytes(int n);
void readFully(byte b[]);
byte readByte(byte b[], int off, int len);
}
• Use the interface type as a parameter for a method: inside
the method, you can invoke any of the methods promised
by the interface parameter, e.g.
void howBigIsIt(Comparable c) {
… Object somethongElse = …
if ( c.compareTo(somethingElse) ) … }
Why Multiple Inheritance?
Java Applet
Java Thread
Your Applet:
a multithread
applet talking
to the server
Java Socket
Some examples
public interface TelDir {
TelNo name_to_tel (String name);
String tel_to_name (TelNo x);
}
public interface URLdir {
IP url_to_ip (Url x);
Url ip_to_url (IP y);
}
public class Agent extends Some_class implements TelDir, URLdir {
TelNo name_to_tel (String name) {…..};
String tel_no_name (TelNo x) {……};
IP url_to_ip (Url x) {…..};
Url ip_to_url (IP y) {….};
…….
}
public interface Comparable {
public int compareTo(Object o);
}
Compare this object with o.
Return –1, 0, +1 if this object
is <, =, > o respectively
I want to write a general
sorting method!
public void my_sorting (int [] temp){….};
// not general //
public void my_sorting (Object [] temp){….};
// too general //
public void my_sorting (Comparable [] temp){….};
public class Type_A
implements Comparable{
…….
};
public class Type_B
implements Comparable{
…….
};
……
Type_A x[] = new Type_A[5];
Type_B y[] = new Type_B[5];
…….
my_sorting(x);
my_sorting(y);
// OK //
Note: defining an
interface is
defining a new
reference data
type.
So, interface can
be used anywhere
that a data type
can be used.
my_sorting
public void my_sorting(Comparable[] temp)
{
int i, j; Object t;
for ( i=0; i<temp.length-1; i++ )
for ( j=i+1; j<temp.length; j++ ) {
if ( temp[i].compareTo(temp[j]) > 0 ) {
t = temp[i];
temp[i] = temp[j];
temp[j] = t;
Interface Comparable is
}
implemented by classes
}
}
Integer, Float, Long, ….