MATLAB Object-Oriented Programming

(Joyce) #1
Concrete methods defined by superclasses in a heterogeneous hierarchy must specify the
Sealed attribute. Sealing these methods prevents subclasses from overriding methods
implemented by the superclass. When calling methods on a heterogeneous array,
MATLAB calls the methods defined by the class of the array (Assets in this example).

The pie and makeReport methods are examples of sealed methods that operate on
heterogeneous arrays composed of Stock, Bond, and Cash objects.

Abstract methods defined by the superclasses in a heterogeneous hierarchy must specify
the Abstract attribute. Defining an abstract method in a superclass ensures that
concrete subclasses have an implementation for that exact method name. Use these
methods element-wise so that each object calls its own method.

The getCurrentValue method is an example of an abstract method that is implemented
by each subclass to get the current value of each asset.

Each type of subclass object calculates its current value in a different way. If you add
another category of asset by adding another subclass to the hierarchy, this class must
implement its own version of a getCurrentValue method. Because all subclasses
implement a getCurrentValue method, the pie and makeReport methods work with
newly added subclasses.

For more information on the Sealed and Abstract method attributes, see “Method
Attributes” on page 9-5.

Assets Class Code

The Assets class and other classes in the hierarchy are contained in a package called
financial.

classdef Assets < matlab.mixin.Heterogeneous
% file: +financial.@Assets/Assets.m
properties
Description char = 'Assets'
end
properties (Abstract, SetAccess = private)
Type
end
methods (Abstract)
% Not implemented by Assets class
value = getCurrentValue(obj)
end
methods (Static, Sealed, Access = protected)

20 Designing Related Classes

Free download pdf