170 likes | 284 Views
Summary of interface concepts from K.P. Chow, University of Hong Kong, including data and operations implementation, with insights on implementation methods, operations, usage scenarios, and comparison with abstract classes.
E N D
Java Interface(สรุป) from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)
Data (including implementation) + Operations implementation Still remember? Users A set of interface Interface Interface A collection of method definitions (without implementation) Defines a set of methods without implementing them
Interface Declaration IsEmpty( ) Set Exists(x) Data:{a, b, c, …..} Operation Implementation Details Methods Declaration Users Interface body Add(x) Constant Declaration Delete(x) 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)
Implement all methods declared in the interface (& its superinterfaces) Can implement > 1 interfaces Implement all these methods 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){……..}; }
Interface vs Abstract Class A subclass can only inherit from ONE superclass One class can implement >1 interfaces 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 Java Socket Your Applet:a multithread applet talking to the server
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) {….}; ……. }
I want to write a general sorting method! public interface Comparable { public int compareTo(Object o); } Compare this object with o. Return –1, 0, +1 if this object is <, =, > o respectively // not general // public void my_sorting (int [] temp){….}; // too general // public void my_sorting (Object [] temp){….};
…… Type_A x[] = new Type_A[5]; Type_B y[] = new Type_B[5]; ……. my_sorting(x); my_sorting(y); 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{ ……. };
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, ….