Interface Inheritance

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

4 min read

Interfaces support inheritance. For example, we could extend our Clock interface to support the concept of an alarm clock:

Slice
module M
{
interface AlarmClock extends Clock
{
TimeOfDay getAlarmTime();
void setAlarmTime(TimeOfDay alarmTime);
}
}

AlarmClock inherits all the operations of Clock: an AlarmClock object provides the getTime and setTime operations in addition to its own getAlarmTime and setAlarmTime operations.

Multiple interface inheritance is also possible. For example, we can construct a radio alarm clock as follows:

Slice
module M
{
interface Radio
{
void setFrequency(long hertz);
void setVolume(long dB);
}
enum AlarmMode { RadioAlarm, BeepAlarm }
interface RadioClock extends Radio, AlarmClock
{
void setMode(AlarmMode mode);
AlarmMode getMode();
}
}

RadioClock extends both Radio and AlarmClock, so a RadioClock* proxy can be used where a Radio*, an AlarmClock*, or a Clock* proxy is expected. The inheritance diagram for this definition looks as follows:

Radio clock interface inheritance RadioClock inherits from both Radio and AlarmClock. AlarmClock in turn inherits from Clock. Hollow triangular arrowheads point from each derived interface toward its base interface. INTERFACE Clock INTERFACE Radio INTERFACE AlarmClock INTERFACE RadioClock

Radio clock interface inheritance.

Interfaces that inherit from more than one base interface may share a common base interface. For example, the following definition is legal:

Slice
interface B { /* ... */ }
interface I1 extends B { /* ... */ }
interface I2 extends B { /* ... */ }
interface D extends I1, I2 { /* ... */ }

This definition results in the familiar diamond shape:

Diamond interface inheritance D inherits from I1 and I2, which both inherit from B. Hollow arrowheads point toward each base interface. INTERFACE B INTERFACE I1 INTERFACE I2 INTERFACE D

All the operations of an interface, including the ones it inherits, must have distinct names, and names that differ only in capitalization count as the same name. An operation inherited several times through a common base interface, as in the diamond above, counts once. For example, the following definition is illegal:

Slice
interface Clock
{
void set(TimeOfDay time); // set time
}
interface Radio
{
void set(long hertz); // set frequency
}
interface RadioClock extends Radio, Clock // Illegal!
{
// ...
}

All Slice interfaces are ultimately derived from Object. For example, the radio clock inheritance hierarchy shown earlier would be shown more correctly as:

Implicit inheritance from Object Radio and Clock implicitly inherit from Object. AlarmClock inherits from Clock, and RadioClock inherits from Radio and AlarmClock. Hollow arrowheads point toward the base interfaces. INTERFACE Object INTERFACE Clock INTERFACE Radio INTERFACE AlarmClock INTERFACE RadioClock Implicit inheritance Implicit inheritance

Because all interfaces have this common base interface, an Object* parameter accepts a proxy of any type. For example:

Slice
interface ProxyStore
{
idempotent void putProxy(string name, Object* o);
idempotent Object* getProxy(string name);
}

Object is a Slice keyword (note the capitalization) that denotes the root interface of the inheritance hierarchy. Inheritance from Object is always implicit: an interface cannot list Object as a base interface.

Every interface inherits the four operations of Object: ice_ping, ice_isA, ice_id, and ice_ids.