119 of 208
ZBasic Language Reference 111 ZX Microcontroller Family Abstract Sub Identify() Debug.Print "Howdy, "; End Sub Sub Identify() Call MyObject::Identify() Debug.Print "I'm an A, size="; m_size End Sub 4.13 Final Classes The classes described thus far have the characteristic that they may be (or, in the case of Abstract classes, must be) extended. It is also possible to define a class that may not be extended. This is done by using the Final attribute preceding the Class keyword. Class A Public: Sub Identify() End Sub Protected: Dim m_size as Integer Dim m_weight as Integer Private: End Class Final Class B Extends A Public: Sub Identify() End Sub Protected: Private: End Class 4.14 Using Mixins Earlier in this chapter, the concept of inheritance (defining a class that inherits functionality from another class) was discussed. Also discussed was the concept of composition (defining a class containing one or more data members that are other objects). To review, if class B is defined as inheriting from class A, then class B is said to satisfy the “is a” relationship (sometimes written as ISA) with respect to class A. This means that an object of type class B can be treated as if it is, in fact, an object of type class A. The public and protected methods and data members of class A are available to all methods of class B as if they were directly part of the definition of class B, etc. In contrast, if class B is defined with a data member of type class A (i.e. using composition), then class B would be said to satisfy the “has a” relationship (sometimes written as HASA) with respect to class A. Only the public methods and data members of class A would be available to the methods of class B and the access would need to be qualified using the name of the data member. The concept of a mixin combines aspects of both inheritance and composition. If class B includes class A as a mixin, then the public methods and data members of class A are accessible to the methods of class B as if they were directly part of the definition of class B (as with inheritance) but class B satisfies the HASA relationship with respect to class A (as with composition). Because you can specify multiple mixin classes, doing so provides some of the benefits of multiple inheritance but without having the difficult issues that arise with traditional multiple inheritance.