Repository navigation
Inheritance
In Minima, records can inherit base records through the extend keyword. Note that unlike Java and C++, Minima does NOT support multi-base inheritance. This practice is done in an effort to discourage constructor-chaining and make your Minima code more extensible/flexible. Ironically, inheritance in Minima is implemented through composition
Note: You should read up on records before reading this page on inheritance.
Copy and paste this code into Minima, it works!
record person { rem this is the base-class
name
age
proc init(name, age) {
set this.name to name
set this.age to age
}
proc can_drink {
return this.age > 21
}
proc print {
extern printl("Name: ", this.name)
extern printl("Age: ", this.age)
}
}
record student extends person { rem this is the derived-class
grade
gpa
proc init(name, age, grade, gpa) {
goproc init as this.base(name, age)
set this.grade to grade
set this.gpa to gpa
}
proc is_smart {
return this.gpa >= 4.0
}
proc print {
goproc print as this.base
extern printl("Grade: ", this.grade)
extern printl("GPA: ", this.gpa)
}
}
set michael to new student("Michael", 15, 9, 4.0)
Use the extend keyword to make a derived-record inherit a base-record. A derived-record that inherited a base-record...
- Inherits the base-records properties. In the example above,
michaelwould also have anameandageproperty which it inherited fromperson. However, shouldstudentdefine its ownnameandageproperty, it's base class -person's -nameandage property would have to be accessed through... *this.base.nameandthis.base.agefrom within one ofstudent's sub-procedure *michael.base.nameandmichael.base.age``` from outside. - Inherits the base-records methods. In the example above
studentinherits all ofperson's sub-procedures (can_drink, andprint). That means you can callgoproc can_drink as michael, and it would work -studentwhich is aperson- any student object (i.e.michael) can call any procedure defined instudent. However, apersonobject cannot use any ofstudent's sub-procedures becausepersonis NOT astudent. Note: that once a class already has a base-class attached to it, it can no longer inherit/extend any more base-classes.
To override a base-record sub-procedure, simply define a sup-procedure in the derived-class. However, to call a keyword, you must invoke the base record's method through the this.base keyword - which simply gets an instance of the current derived-record as it's base-record's type. In the above example, michael, a student object, calls a person sub-procedure print through the this.base keyword.