CSC152 2005F, Class 29: Parameterized Classes III Admin * Have a great break (after class) Overview * Review * New problem: Generalizing to RealVariables * A different solution: Parameterized Classes * Another application: Comparison Monday: Exceptions Tuesday: Supposedly started parameterized classes * Context (1): Working with IntegerVariable objects * Context (2): Exponentiation * Goal of generalization: Whatever we can do with IntegerVariables, we can also do with related types Wednesday: Supposedly started parameterized classes * How polymorphism helps us generalize * Create an interface which describes general characteristics (NumericVariable) * ... Good programmers often start with the general form rather than generalizing the specific form Status: * Wrote expt2 * Generalized using interfaces New problem: * Does it work with RealVariables? * Not immediately * Need to make it implement NumericVariable * Therefore need to add intValue() * Also need to add create(int init) * Whoops! That's not very comfortable * But if we add a create(real init), we need to go back to change IntegerVariable and BigIntegerVariable Rather than this kind of polymorphism, we need PARAMETERIZED polymorphism. * Originally: procedure works with any class that provides multiply(NumericVariable), that is, a method that takes the generalized interface as a parameter * We instead asay that exponent works as long as multiply takes the same type as the iobject doing the multiplication and returns the same type We care that the multiply method in class X can take an X as a parameter and return an X as a value. * In Java, we can add *parameters* to interfaces and to classes Parameters can be replaced by types when constructing and implementinga Why do we care about parameterized types? Sorting! (and much more) public void sort(Comparator c, Vector vov)