CSC152 2004F, Class 11: Interfaces (Finally) Admin: * Missing: Eryn, Louisa, Mark * Really, really, late: Michael B. * Office hours in early afternoon this week. * Sam will try to remember to put homework *in the homework folder* so that you can find it. (There was no homework for today, there is homework for tomorrow.) * Stop me when things get unclear. Overview: * Generalizing points/polarpoints * The Java interface: Method names without instructions or fields. * Classes using interfaces. * Classes implementing interfaces. Reminder: * Suppose we could say "these kinds of objects provide these methods, with these semantics" * E.g., TwoDimVec and NewTwoDimVec both provide getX and getY with semantics "If this vector is interpreted within the euclidean plane, the coordinates of the tip are at x,y" * Given the knowledge that both kinds of objects "behave the same way", we can use them in common ways. * For example, if we have to print an object or either type, we can use pen.println("(" + vec.getX() + "," + vec.getY() + ")"); * In an ideal world, Java should be able to figure out which methods each object provides. * That "ideal" ignores semantics. * Consider an "CaeserCypher" object. It might also have getX and getY. * Java can't tell us whether or not it's sensible to call getX and getY. /The Solution: Interfaces/ * Programmer1 specifies a general group/set/collection of methods that objects might implement. * These methods naturally go together. * Programmer does not specify how they are implemented. * Programmer2 can then write code that uses those methods (not knowing how they are implemented). * Programmer3 can write a class that implemetns those methods. * Programmer4 can write a different class that impleemnts those methods. * Outcome: Programmer2's code should work with the class designed by programmer 3 or Programmer 4. Interfaces specify groups of methods. Form public interface NAMEOFINTERFACE { public TYPE METHOD_ONE(PARAMS); public TYPE METHOD_TWO(PARAMS); public TYPE METHOD_THREE(PARAMS); public TYPE METHOD_FOUR(PARAMS); } // interface NAMEOFINTERFACE No *body* for method declarations. Using these things: * Use the interface name where you would previously use a class name. * You may refer to any of the methods specified in the interface. Building classes from interfaces. * The classes specify a particular implementation of the interface. * Interface = "ADT"; Class = "Data Structure" (approximately) public class NAMEOFCLASS implements NAMEOFINTERFACE { CODE MUST INCLUDE CODE FOR ALL METHODS SPECIFIED IN INTERFACE } Morals: * Stages of design * Design interfaces first! These specify what your class (or classes) will eventually do. * Design testers (graduate assistants) next. These test the components of the interface. Good testers also have predicted output. * Design classes that implement interfaces. * Test test test. * A class that implements an interface must implement all methods of that interface. * A class that implements an interface can be substituted for a parameter or variable of that interface. <-- Key idea New Moral: * A class may implement multiple interfaces. Homework: * Consider lists in Scheme * Express them in Java terms through an interface * Warning! Construction may be hard. (Interfaces don't specify constructors.)