Search⌘ K
AI Features

Superclass Members

Explore how to access inherited superclass members in D using the super keyword, disambiguate names in inheritance hierarchies, and properly call superclass constructors from subclasses. Understand the role of super in constructors and how subclass constructors manage superclass initialization for seamless object creation.

Accessing superclass members

The super keyword allows referring to members that are inherited from the superclass.

D
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' member
minute = 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 ...