Skip to content

Ch 2. Make Making Rooms Section 4 Coding Excursus 2 – Inheritance

Rene Orion Salmon edited this page May 12, 2020 · 1 revision

2.4 – Coding Excursus 2 – Inheritance

Last coding excursus, we noted that each object definition had to contain a list of one or more classes. These classes are known as object inherits. Classes can also inherit from one or more classes. Our custom DarkRoom class inherited these traits from the Room classes. Though we haven’t looked at these classes yet, the Room class inherits from both the TravelConnector and Thing classes, respectively. In this case, inheriting from more than one class is called multiple inheritance.

Classes are useful when you want certain objects to behave alike. It would be rather annoying if you were to have to code the same things repeatedly for similar objects. So, we can use inheritance to take care of said properties like in the last section, we inherited from the Room class.

However, like we’ve seen before, sometimes we’d want to make a different kind of room that doesn’t come packaged with adv3Lite. In our case, we made a DarkRoom class, as shown below.

pic

Note that in TADS, classes and objects are treated similarly, but there are some key differences, so if we mean to something to be used as a class, we must define it as a class.

Also take note that Rooms aren’t the only kind of object that have the isLit property. You could’ve also gone about it differently by defining a Dark class like this:

pic

Then you could’ve defined the dark room as such:

pic Of course, no one would define it like this, but its just to demonstrate that it is possible. However, TADS is a lot more sophisticated than that, so rather than define classes as such, we can simply define our object as inheriting from multiple classes and then lead TADS to work out how all the classes will actually work together.

99% of the time, we’ll get the behavior we want if we follow this rule: mix-in classes must always come first. A mix-in class is like our Dark class that modifies the behavior of other classes, but doesn’t define a full set of behavior itself. This type of class is generally any class that doesn’t inherit from Thing. We’ll look at the Thing class in the next chapter.