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:

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>.