Superclass Members
Get to learn how to access and construct superclass members.
We'll cover the following...
Accessing superclass members
The super
keyword allows referring to members that are inherited from the superclass.
Press + to interact
class Clock {int hour;int minute;int second;void adjust(int hour, int minute, int second = 0) {this.hour = hour;this.minute = minute;this.second = second;}}class AlarmClock : Clock {int alarmHour;int alarmMinute;void adjustAlarm(int hour, int minute) {alarmHour = hour;alarmMinute = minute;}void foo() {super.minute = 10; // The inherited 'minute' memberminute = 10; // Same thing if there is no ambiguity}}void main() {}
The super
keyword is not always necessary; minute alone has the same meaning in the code above (line # 23). The super
keyword is needed when both the superclass and the subclass have members under the same names.
If multiple classes in an inheritance tree define a symbol with the same name, one can use the specific name of the class in the inheritance tree to disambiguate between the symbols:
Press + to interact
import std.stdio;class Device {string manufacturer;}class Clock : Device {string manufacturer;}class AlarmClock : Clock {// ...void foo() {Device.manufacturer = "Sunny Horology, Inc.";Clock.manufacturer = "Better Watches, Ltd.";}}void main() {auto alarmC = new AlarmClock();alarmC.foo();writeln(alarmC.manufacturer);}
...