next up previous
Next: 1.4.5 Method Overloading Up: 1.4 Java Classes and Previous: 1.4.3.1 An Extended Example

1.4.4 Inheritance and the Composite Pattern

The method

public boolean equals(Object o);
is defined in the special Object that is a superclass of all Java classes. Any Java class that is defined without a specific superclass is an immediate subclass of the Object class. In the class Object, equals is defined to mean Object identity. For some classes, identity and equality are identical notions, but for many others they are not. In the class String, the equals method is redefined to compare the sequences of characters in strings, so that copies of the same string are considered equal. The redefinition of equals only affects the class String and any subclasses that it might have. This selective form of method redefinition is called method overriding.

We have already seen a restricted form of method overriding in our DeptDirectory example. When an abstract method from an abstract class is overridden in a concrete subclass (e.g. findOffice from DeptDirectory in the classes Emtpy and Cons), the ``missing'' definition inherited from the abstract class is overridden.

When an abstract class like DeptDirectory serves as a superclass for several fundamentally different concrete classes such as Empty and Cons, the class hierarchy forms a composite pattern. The composite pattern is the standard object-oriented representation for a data definition that includes more than one form for data. The defined data type is an abstract class called the composite class where each specific form for the data type is a concrete subclass called a variant class.

All inductive data definitions (data definitions with that refer to themselves) have this form because an inductive definition must have at least one base form and one inductive form for the data. If the base form is omitted, the defined data type is empty because there is no base case to get the inductive data generation process started!


next up previous
Next: 1.4.5 Method Overloading Up: 1.4 Java Classes and Previous: 1.4.3.1 An Extended Example
Robert Cartwright, Spring 1999