MATLAB Object-Oriented Programming

(Joyce) #1
Heterogeneous hierarchies enable you to define the default object for that hierarchy. The
matlab.mixin.Heterogeneous class provides a default implementation of a method
called getDefaultScalarElement. This method returns an instance of the root class of
the heterogeneous hierarchy, unless the root superclass is abstract.

If the root superclass is abstract or is not appropriate for a default object, override the
getDefaultScalarElement method. Implement the getDefaultScalarElement
override in the root superclass, which derives directly from
matlab.mixin.Heterogeneous.

getDefaultScalarElement must return a scalar object that is derived from the root
superclass. For specific information on how to implement this method, see
getDefaultScalarElement.

Conversion During Assignment and Concatenation


If you create a heterogeneous array that contains objects that are not derived from the
same root superclass, MATLAB attempts to call a method called convertObject.
Implement convertObject to convert objects to the appropriate class. There is no
default implementation of this method.

To support the formation of heterogeneous arrays using objects that are not part of the
heterogeneous hierarchy, implement a convertObject method in the root superclass.
The convertObject method must convert the nonmember object to a valid member of
the heterogeneous hierarchy.

For details on implementing the convertObject method, see
matlab.mixin.Heterogeneous.

Empty Arrays of Heterogeneous Abstract Classes


For homogeneous arrays, MATLAB does not allow you to initialize an empty array of an
abstract class. However, if the class is part of a heterogeneous hierarchy, you can
initialize empty arrays of an abstract class. Initializing an empty heterogeneous array is
useful in cases in which you do not know the class of the concrete elements in advance.

For example, suppose RootSuperclass is an abstract class that is the root of a
heterogeneous hierarchy. Initialize an array using the empty static method:

ary = RootSuperclass.empty;

10 Object Arrays

Free download pdf