118 of 208
ZBasic Language Reference 110 ZX Microcontroller Family It is important to understand that when a class is derived from another class, the compiler generates code to ensure that the base class object is fully initialized before the constructor for the derived class executes. Similarly, for assignment constructors the base class assignment constructor is executed before that of the derived class. This order guarantees that the derived class object may safely access the methods and members of the base class in its constructors. The situation with destructors is similar but the order is reversed. In the case of destructors the derived class destructor executes before the base class destructor. This order guarantees that the derived class may rely on the integrity of the base class data members if necessary. One aspect of inheritance that must be carefully considered is that since the base class constructor executes before a derived class constructor, base class constructors must avoid invoking methods that would result in the derived class method executing before derived class’ constructor has executed. 4.12 Abstract Classes, Abstract Methods When designing an object-oriented solution, it is often useful to define a class that embodies some essential characteristics and functions that can then be inherited by several other classes. In some situations, the base class is “incomplete” in the sense that its only purpose is to serve as the base class for other classes. You can prevent such an incomplete class from being instantiated (i.e. defining an object of that type) by defining the base class to be abstract. This is done by adding the Abstract attribute to the class definition. Abstract Class MyObject Public: Protected: Dim m_size as Integer Dim m_weight as Integer Private: End Class If you try to create an object of the class MyObject, the compiler will produce an error message indicating that you cannot instantiate an abstract class. This is to remind you that the purpose of the class is as a building block for other classes. You can also indicate that a class is abstract by defining the class with at least one abstract method as in the class definition below. Class MyObject Public: Abstract Sub Identify() End Sub Protected: Dim m_size as Integer Dim m_weight as Integer Private: End Class The result of both of the preceding class definitions is an abstract class. The difference is that when you define abstract methods, in addition to making the class abstract it also establishes a requirement that all derived classes must define a method that has the same signature, i.e. the same name and same number and types of parameters. If you wish, you may include code in the body of the abstract method. One reason for doing so would be to define some essential functionality that many or most derived classes could use. The derived class method can invoke the abstract base class method using the class namespace prefix or the parent class prefix. For example, class A could be derived from MyObject and the Identify() methods of the MyObject and A classes could be rewritten as shown below, respectively. You should compile and run such and example to observe the result.