1 / 11

Java Interface( สรุป)

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).

shows
Download Presentation

Java Interface( สรุป)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Interface(สรุป) from K.P. Chow University of Hong Kong (some slides from S.M. Yiu)

  2. 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

  3. 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)

  4. 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){……..}; }

  5. 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

  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 Applet:a 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. 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){….};

  10. …… 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{ ……. };

  11. 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, ….

More Related