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:
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:
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.
Interfaces that inherit from more than one base interface may share a common base interface. For example, the following definition is legal:
interface B { /* ... */ }interface I1 extends B { /* ... */ }interface I2 extends B { /* ... */ }interface D extends I1, I2 { /* ... */ }This definition results in the familiar diamond shape:
Interface Inheritance Limitations
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:
interface Clock{ void set(TimeOfDay time); // set time}
interface Radio{ void set(long hertz); // set frequency}
interface RadioClock extends Radio, Clock // Illegal!{ // ...}Implicit Inheritance from Object
All Slice interfaces are ultimately derived from Object. For example, the radio clock inheritance hierarchy shown earlier would be shown more correctly as:
Because all interfaces have this common base interface, an Object* parameter accepts a proxy of any type. For example:
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.