BOOK NOTES CHAPTER 11 INTERFACES AND POLYMORPHISM 11.1 USING INTERFACES FOR CODE REUSE - make code more general and reusable - focus on the essential operations - interface type - express common operations - specify required operations - interface declaration lists all methods that the interface requires - can declare your own interfaces - can treat as a type - differences from a class - interface methods - abstract - have name, parameters, return type - lack implementation - automatically public - lacks instance fields - use as a data type - must provide a real class for that kind of object - class must implement the interface - declare the interface in an implements clause - syntax: public class implements - implement all of the methods required by the interface - class can implement multiple interfaces - must define all methods in all interfaces that implements - needn't declare methods public in interface, but must in implementing class - can declare public in interface, so recommend always do - interface expresses what a certain set of objects have in common - increases reusability - 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 - defining an inteface syntax public interface { } AT 11.1 CONSTANTS IN INTERFACES - interfaces cannot have instance fields - interfaces may have constants - all fields are automatically public static final - you may omit these modifiers - use the constant - with the name of the interface - with the name of a class that implements that interface 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 - you do not know the actual type of a variable declared as an interface type - cannot convert a class that does not implement an interface to that interface type - when an object is stored as an interface type, you cannot use any of it's instance fields or methods not in the interface - requires a cast to convert from an interface type to a class type - if you cast an object to a class that it actually is not, you will get an error - 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 CE 11.2 TRYING TO INSTANTIATE AN INTERFACE - can define variables whose type is an interface - cannot construct an interface - there are no objects whose types are interfaces - an interface variable refers to an object of a class that implements that interface 11.3 POLYMORPHISM - multiple classes implement same interface: each does so in a different way - legal and common to have a variable whose type is an interface - object to which interface variable refers does not have the interface as its 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 - an interface variable can refer to objects of different types throughout its lifetime - 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 - 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 works for objects of many types, and depends on the actual type of the object - all Java instance methods - 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: compiler knows which method is executed before the program runs - polymorphism - late binding - the virtual machine selects which method is run - the compiler cannot necessarily determine which kind of object will be used as an interface 11.4 USING INTERFACES FOR CALLBACKS - can only add an interface to classes under your control - cannot make some classes implement another interface - define an interface where classes of this interface will implement interface functionality for objects of other classes - one new class implementing an interface for this purpose for each class which you would have had implement the original application of interfaces - method in the second kind of interface - call back: call to this kind of method - use type Object as the only parameter - because all objects are of type Object - must explicitly cast parameter to type actually using - allows decoupling - class only depends on the callback interface - does not depend on the objects it manipulates via that interface - class implementing the callback interface - small - "helper" 11.5 INNER CLASSES - class that serves a very limited purpose - declare the class inside the method that needs it - inner class: any class that is defined inside another class - signals that the class is not interesting beyond the scope of the method - not publicly accessible - doesn't need as much documentation - if defined within a class but outside a method, available to all methods in enclosing class - compiler turns inner class into a regular class file 11.6 PROCESSING TIMER EVENTS - using interfaces - easily extensible to GUIs - javax.swing.Timer class - generates a series of events at fixed intervals - useful for regularly updating an object - call a method whenever the time goes off - ActionListener interface - package java.awt.event - has method void actionPerformed(java.awt.event.ActionEvent event) that is called whenever the timer goes off - event listener: notified when a particular event occurs - to use timer - define a class that implements ActionListener - place action you want to occur inside actionPerformed implementation - construct object of that class Timer(int millisecondsDelay, ActionListener listener) - pass it to Timer constructor - start the timer - actionPerformed method - is a callback - parameter - contains more detailed info about the timer event - often ignored CE 11.3 MODIFYING THE SIGNATURE IN THE IMPLEMENTING METHOD - when implementing interface, must define each method exactly as specified in interface - otherwise, causes a compiler error: will say that your class does not correctly implement the interface 11.7 ACCESSING SURROUNDING VARIABLES - inner classes can access variables that are defined in surrounding blocks - block: statement group enclosed by braces - when a block is nested inside another, the inner one can access all variables defined in the outer one - methods of an inner class can access the variables from the enclosing scope - avoid passing methods as parameters - keep classes seperate from classes that manipulate them via interfaces - inner class can only access surrounding local variables if they are declared final - a final reference variable must always refer to the same object, but that object can change - inner class can access fields of the surrounding class - field must belong to the object that constructed the inner class - if inner class object created inside a static method, can only access static surrounding fields - to build a GUI, need an event listener associated with every component that the user can manipulate