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.
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.
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.
Each subclass in the heterogeneous hierarchy implements a concrete version of the abstract method. The concrete method provides specialized behavior required by the particular subclass.
Printable class shows how to implement a template method approach:
classdef (HandleCompatible) Printable methods(Sealed) function print(aryIn) n = numel(aryIn); for k=1:n printElement(aryIn(k)); end end end methods(Access=protected, Abstract) printElement(objIn) end end