CSC152 2005S, Class 24: Inheritance Admin: * Exams due! (Returned next week.) * Sam gone next week. Sorry. Cassie runs labs. * Oh no! Another lecture. Overview: * Reuse * Polymorphism * Inheritance * Examples * Abstract classes /Reuse/ * Object-oriented programming is intended, in part, to help in the engineering of large software projects * Big problem in large projects: Reuse * In practice, most of the time we seem to start "from scratch" * Ways we can reuse stuff in object-oriented languages * Reuse well-designed objects and classes * Encapsulation helps * Java's huge library of classes is one instance of this kind of reuse * Javadoc encourages you to document your classes for reuse * Polymorphism suggests that we can reuse methods on "similar" objects * Inheritance suggests that you can define new classes in terms of old classes /Polymorphism/ * You can use similar things in similar situations * For example, once we've written code to print something of multiple lines: for (int i = 0; i < thing.getLines(); i++) pen.println(thing.getLine(i)); * That code, if cut and paste, will work with any "thing" that provides getLine(int) and getLines() with the appropriate "semantics" * In Java, we can formalize this in a method and interface public void printThing(TextBlock thing) { for (int i = 0; i < thing.getLines(); i++) pen.println(thing.getLine(i)); } // printThing(TextBlock) * Note: TextBlock must be an interface that provides getLines() and getLine(int) * Note: We can only call printThing using somethign that implements TextBlock * Great from reuse perspective: * This not only works on everything that currently i9mplements TextBlocks * It also works on anything written in the future that implements TextBlock * We will regularly rely on polymorphism in this class * Abstract data types will be represented as interfaces * Particular data structures will be represented as classes that implement those interfaces /However, there are other kinds of reuse we like/ * This class is just like that class, except one method behaves differently * This class is just like that class, except we add one method in this class * E.g., Consider a point in the plane * Normally have getX, getY, getDistanceFromOrigin * Someone might want to add getTaxicabDistance * Someone else might want to always use the alternate version in place of getDistanceFromOrigin * Inheritance provides this kind of convenient reuse * We build a new class in terms of another class * Java syntax public class NewClass extends OldClass { } * NewClass automatically gains all fields and methods of OldClass * NewClass automatically implements anything OldClass impelments * NewClass automatically extends anything OldClass extends * NewClass does NOT get the constructors of OldClass * NewClass *can* add new fields and methods * NewClass *can* redefine any methods of OldClass * An object in class NewClass can be used in place of an object in class OldClass * But the redefined methods will be used on the object in class NewClass * Potential problem: If you change OldClass (e.g., to add an exception to some method), NewClass may break. * Don't change OldClass! /Writing constructors in subclasses/ * You need to write them! * If you want to call a constructor in a superclass, you must use super(...) * The 'super' means "the corresponding constructor in the superclass" * If you don't call a constructor in the superclass, Java will decide that you called the zero-parameter constructor anyway.