Class Inheritance
2 min read
2 min read
2 min read
2 min read
2 min read
2 min read
2 min read
2 min read
2 min read
Simple Inheritance
Unlike structures, classes support inheritance. For example:
Slice
module M{ class TimeOfDay { short hour; // 0 - 23 short minute; // 0 - 59 short second; // 0 - 59 }
class DateTime extends TimeOfDay { short day; // 1 - 31 short month; // 1 - 12 short year; // 1753 onwards }}This example illustrates one major reason for using a class: a class can be extended by inheritance, whereas a structure is not extensible. The previous example defines DateTime to extend the TimeOfDay class with a date.
Classes only support single inheritance. The following is illegal:
Slice
class TimeOfDay{ short hour; // 0 - 23 short minute; // 0 - 59 short second; // 0 - 59}
class Date{ short day; short month; short year;}
class DateTime extends TimeOfDay, Date // Error!{ // ...}A derived class cannot redefine a field of its base class:
Slice
class Base{ int integer;}
class Derived extends Base{ int integer; // Error, integer redefined}Implicit Inheritance from Value
All classes implicitly inherit from Value. This way, a Value parameter in an operation accepts any class instance.