LECTURE NOTES CHAPTER 11 INTERFACES FOR CODE REUSE - make code more general and reusable - focus on essential operations common to a group of classes - e.g. GUI - objects? - window - button - link - dropdown menu - text box - common operations? - onClick() - draw() - as classes, these would all implement a common *interface*, GUIable - interface type: express/require a set of common operations - declaration lists all required methods - can declare your own interfaces - syntax (defining an inteface): public interface { } - naming convention: end name with -able - method signatures means put ; at end instead of {} - e.g. public void draw(); - can treat as a type - differences from a class - no instance fields (remember: common *operations*) - interface methods - abstract - have name, parameters, return type - lack implementation - automatically public (don't have to declare as public, but can) - interfaces are non-instantiable (can't create instances) - cannot construct an interface - there are no objects whose types are interfaces - nevertheless, can define variables whose type is an interface - how to use as a data type - programs can only manipulate objects - interfaces are non-instantiable - sol'n: create a class that implements the interface - means class must provide implementations of interface methods - create objects of this class - can then call interface methods on objects of class - e.g. - can't have an "onClick", can only have something that does onClick - create a Button class - Button implements our GUI interface - means that it has to provide onClick and draw behaviors - think of type of these objects as *both* - that of the class AND - that of the implemented interface - means can put this object in a variable that has that interface as it's type - syntax (creating a class that implements an interface): public class implements - remember to include all interface methods in the class! - e.g. public class Menu implements GUIable { public void onClick() { // do something } public void draw() { // do something } // Menu-specific methods here public void dropDown() { // do something } } - class can implement multiple interfaces - e.g. - also Measurable: maybe a button's measure is it's area - independent of whether or not it's GUIable - how to add to above? - must define *all* methods in *all* interfaces that implements - mnemonic: when you say "implements" it's a contract... you're saying you'll provide an implementation of all methods in the interface, so if you don't, it's a compiler error - needn't declare methods public in interface, but must in implementing class - UML - tag box with <> - dotted arrow with triangle tip denotes is-a relationship - from more to less specialized - classes that implement an interface have an is-a relationship with that interface - reduces coupling because a given class may only depend on the interface, instead of directly upon all classes that implement that interface CONSTANTS IN INTERFACES - how are interfaces different from classes? - interfaces cannot have instance fields - interfaces may have constants - all fields are automatically public static final - you may omit these modifiers - e.g. int MIN = 0; // automatically public static final - use the constant - with the name of the interface - with the name of a class that implements that interface - e.g. - interface Rootable (can take square root of) - class NaturalNumber implements Rootable - Rootable.MIN // evaluates to 0 - NaturalNumber.MIN - also evaluates to 0 - *needn't declare in NaturalNumber* 11.2 CONVERTING BETWEEN CLASS AND INTERFACE TYPES - interfaces express commonality between classes - legal to convert from a class type to the type of any interface that the class implements - e.g. GUIable g1 = new Button(); // OK - you do not know the actual type of a variable declared as an interface type - e.g. GUIable g2 = new Menu(); // also OK - so a GUIable variable could contain any kind of class that implements GUIable... thus, just given a variable gn that was declared GUIable, we have no idea the class of the object it holds - cannot convert a class that does not implement an interface to that interface type - e.g. public class Michelle {...} GUIable g3 = new Michelle() // NOT OK (why?) - when an object is stored as an interface type, you cannot use any of it's instance fields or methods not in the interface - e.g. - suppose Menu has a dropDown() method and implements GUIable g2.draw(); // always OK g2.dropDown(); // never OK - why not OK? - don't know the actual class - what if g2 is a Button? - requires a cast to convert from an interface type to a class type - e.g. Button b = (Button) g1; - if you cast an object to a class that it actually is not, you will get an error (exception) - mnemonic: cast is your way of telling the compiler you accept responsibility for the bad things that could happen - distinction between casting numbers and objects - numbers - possible to lose information - purpose: tell compiler you agree to that loss - objects - risk causing an exception - purpose: tell compiler you agree to risk causing an exception - review: which are OK? GUIable g = new GUIable(); // no - interface isn't instantiable GUIable g = new Button(); // yes, if Button implements GUIable Button b = new GUIable(); // no - interface isn't instantiable Button b = new Button(); // yes (as usual) POLYMORPHISM - multiple classes implement same interface: each does so in a different way - e.g. for web forms - Button click submits data to server - Menu click calls dropDown to display rest of menu - review: legal and common to have a variable whose type is an interface - what is my type? - technically, object to which interface variable refers does not have the interface as its "official" type - no object can have an interface as its type - an object can only have a a class that implements an interface as its type - but, now the type of a variable at what goes in it don't have to match - e.g. GUIable g = new Button(); - g's type is GUIable - and it's getting an object of type Button - new rule: rhs must either have - same type as lhs OR - lhs is an interface that rhs implements - an interface variable can refer to objects of different types throughout its lifetime - e.g. just like int i = 1; Button b = new Button(); Menu m = new Menu(); GUIable g = b; ... i = 2; g = m; - review: what methods can we call on an interface variable? - don't know what the type of the object it refers to - can therefore only call methods of the interface - determine which method gets called - on an interface variable - multiple classes implement the interface, so multiple different implementations of the method exist - e.g. public class Button implements GUIable { public void draw() { System.out.println("antanaclasis: " + "Your argument is sound, nothing but sound."); } ... } public class Menu implements GUIable { public void draw() { System.out.println("paralipsis: We will not speak of all " + "Queequeg's peculiarities here; how he eschewed coffee " + "and hot rolls, and applied his undivided attention to " + "beefsteaks, done rare."); } ... } - JVM dynamically determines the actual type of the object referred to by the interface reference - the method implemented in that class is called - a call to a method on an interface reference can result in different methods being executed, depending on the dynamic type of the interface reference - polymorphism - the actual type of the method determines the method to be called - same computation (method call) works for objects of many types, and depends on the actual type of the object - all Java instance methods are polymorphic - a single method call refers to one of multiple possible method implementations - overloaded methods - compiler determines which method to call by looking at the number and type of the parameters - early binding (compile time): compiler knows which method is executed before the program runs - polymorphism - late binding (run time) - the virtual machine selects which method is run - the compiler cannot necessarily determine which kind of object will be used as an interface