5.2 Class inheritance
We have defined two simple classes, <time-of-day> and <time-offset>. We repeat the definitions here:
// A specific time of day from 00:00 (midnight) to before 24:00 (tomorrow) define class <time-of-day> (<object>) slot total-seconds :: <integer>, init-keyword: total-seconds:; end class <time-of-day>; // A relative time between -24:00 and +24:00 define class <time-offset> (<object>) slot total-seconds :: <integer>, init-keyword: total-seconds:; end class <time-offset>;
There is commonality between the two classes:
Both classes represent a kind of time — they have a conceptual basis in common.
Both classes have a
total-secondsslot — they have structure in common.Both classes need a
decode-total-secondsmethod to convert thetotal-secondsslot to hours, minutes, and seconds — they have behavior in common.
We can use inheritance to model the shared aspects of these two classes directly. We need to define a new class, such as <time>, and to redefine the two classes to inherit from <time>. The <time> class will contain the slot total-seconds, and the other two classes will inherit that slot. We shall redefine the decode-total-seconds method such that its parameter is of the <time> type, which means that it can be called for instances of <time-of-day> and of <time-offset>.




