Adding Methods

A subclass isn't restricted to changing the behavior of its superclass. It can also add completely new methods and fields that are not shared with the superclass. For instance if a class represents a user of a multi-user database, the user class might start off with no access. Secretaries could be given read-only access. Therefore the secretary class would have additional methods to read data that aren't part of the user class.

Data entry clerks might be allowed to read the database and to create new records but not to modify old records. Therefore the data entry class would inherit from the secretary class and have methods to allow adding new records. A supervisor class would inherit from the data entry class and also have methods that allows modification of existing records. Finally a database manager would inherit from supervisor, but have new methods that allowed database managers to change the structure of the database.

As a practical example, recall that the Car class added a numberDoors field and a getNumberOfDoors() method that the MotorVehicle class didn't have.

Previous | Next | Top | Cafe au Lait

Copyright 1997, 1998 Elliotte Rusty Harold
Last Modified October 20, 1998