MATLAB Object-Oriented Programming

(Joyce) #1

Handle-Compatible Classes and Heterogeneous Arrays


In this section...
“Heterogeneous Arrays” on page 12-48
“Methods Must Be Sealed” on page 12-48
“Template Technique” on page 12-48

Heterogeneous Arrays


A heterogeneous array contains objects of different classes. Members of a heterogeneous
array have a common superclass, but can belong to different subclasses. See the
matlab.mixin.Heterogeneous class for more information on heterogeneous arrays.
The matlab.mixin.Heterogeneous class is a handle-compatible class.

Methods Must Be Sealed


You can invoke only those methods that are sealed by the common superclass on
heterogeneous arrays (Sealed attribute set to true). Sealed methods prevent subclasses
from overriding those methods and guarantee that methods called on heterogeneous
arrays have the same definition for the entire array.

Subclasses cannot override sealed methods. In situations requiring subclasses to
specialize methods defined by a utility class, you can employ the design pattern referred
to as the template method.

Template Technique


Suppose that you implement a handle compatible class that works with heterogeneous
arrays. This approach enables you to seal public methods, while providing a way for each
subclass to specialize how the method works on each subclass instance. In the handle
compatible class:


  • Define a sealed method that accepts a heterogeneous array as input.

  • Define a protected, abstract method that each subclass must implement.

  • Within the sealed method, call the overridden method for each array element.


12 How to Build on Other Classes

Free download pdf