MATLAB Object-Oriented Programming

(Joyce) #1
classdef (AllowedSubclasses = ?ClassName) MySuperClass
...
end

Use a cell array of meta.class objects to define more than one allowed subclass:
classdef (AllowedSubclasses = {?ClassName1,?ClassName2,...?ClassNameN}) MySuperClass
...
end

Always use the fully qualified class name when referencing the class name:
classdef (AllowedSubclasses = ?Package.SubPackage.ClassName1) MySuperClass
...
end

Assigning an empty cell array to the AllowedSubclasses attribute is effectively the
same as defining a Sealed class.

classdef (AllowedSubclasses = {}) MySuperClass
...
end

NoteUse only the? operator and the class name to generate meta.class objects.
Values assigned to the AllowedSubclasses attribute cannot contain any other MATLAB
expressions, including functions that return either meta.class objects or cell arrays of
meta.class objects.

Result of Declaring Allowed Subclasses

Including a class in the list of AllowedSubclasses does not define that class as a
subclass or require you to define the class as a subclass. It just allows the referenced
class to be defined as a subclass.

Declaring a class as an allowed subclass does not affect whether this class can itself be
subclassed.

A class definition can contain assignments to the AllowedSubclasses attribute that
reference classes that are not currently defined or available on the MATLAB path.
However, any referenced subclass that MATLAB cannot find when loading the class is
effectively removed from the list without causing an error or warning.

NoteIf MATLAB does not find any of the classes in the allowed classes list, the class is
effectively Sealed. A sealed class is equivalent to AllowedSubclasses = {}.

12 How to Build on Other Classes

Free download pdf