Java Interface(????) - PowerPoint PPT Presentation

About This Presentation
Title:

Java Interface(????)

Description:

Java Interface( ) from K.P. Chow University of Hong Kong (some s from S.M. Yiu) Interface Interface vs Abstract Class Usages of Interfaces To simulate ... – PowerPoint PPT presentation

Number of Views:153
Avg rating:3.0/5.0
Slides: 12
Provided by: HKUC1
Category:
Tags: applet | interface | java

less

Transcript and Presenter's Notes

Title: Java Interface(????)


1
Java Interface(????)
  • from K.P. Chow
  • University of Hong Kong
  • (some slides from S.M. Yiu)

2
Interface
Interface A collection of method definitions
(without implementation)
Defines a set of methods without implementing them
3
Note there is a Set interface in Java
public interface Set_interface boolean
IsEmpty() boolean Exists(Object x) void
Add(Object, x) void Delete(Object, x) int
nonsense 0
(public, abstract)
(public, static, final)
4
A class implements an interface
Inherits the constants
public class example implements Set_interface
boolean IsEmpty() . boolean
Exists(Object x). void Add(Object,
x).. void Delete(Object, x)..
5
Interface vs Abstract Class
A subclass can only inherit from ONE superclass
One class can implement gt1 interfaces
Conceptually, interface wants the classes to
guarantee a set of behavior (services) while
inheritance implies an is-a relationship
6
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) )

7
Why Multiple Inheritance?
Java Applet
Java Thread
Java Socket
Your Appleta multithread applet talking to the
server
8
Some examples
public interface URLdir IP url_to_ip
(Url x) Url ip_to_url (IP y)
public interface TelDir TelNo
name_to_tel (String name) String
tel_to_name (TelNo x)
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) . .
9
public interface Comparable public int
compareTo(Object o)
Compare this object with o. Return 1, 0, 1 if
this object is lt, , gt o respectively
// not general //
public void my_sorting (int temp).
// too general //
public void my_sorting (Object temp).
10
public void my_sorting (Comparable temp).
// OK //
public class Type_A implements Comparable
.
Note defining an interface is defining a new
reference data type. So, interface can be used
anywhere that a data type can be used.
public class Type_B implements Comparable
.
11
my_sorting
  • public void my_sorting(Comparable temp)
  • int i, j Object t
  • for ( i0 ilttemp.length-1 i )
  • for ( ji1 jlttemp.length j )
  • if ( tempi.compareTo(tempj) gt 0 )
  • t tempi
  • tempi tempj
  • tempj t

Interface Comparable is implemented by classes
Integer, Float, Long, .
Write a Comment
User Comments (0)
About PowerShow.com