MATLAB Object-Oriented Programming

(Joyce) #1

Control Sequence of Constructor Calls


MATLAB does not guarantee the sequence in which superclass constructors are called
when constructing a subclass object. However, you can control this order by calling
superclass constructors explicitly from the subclass constructor.

If you explicitly call a superclass constructor from the most specific subclass constructor
(ClassC in the following diagram), then MATLAB calls the most specific subclass
constructor first. If you do not make an explicit call to a superclass constructor from the
subclass constructor, MATLAB makes the implicit call when accessing the object.

Suppose that you have a hierarchy of classes in which ClassC derives from ClassB,
which derives from ClassA. The constructor for a subclass can call only direct
superclasses. Therefore, each class constructor can call the direct superclass constructor:

In cases of multiple inheritance, the subclass constructor can call each superclass
constructor. To ensure that a specific superclass constructor calling sequence is followed,
call all direct superclass constructors explicitly from the most specific subclass
constructor:

12 How to Build on Other Classes

Free download pdf