Specify Allowed Subclasses
Why Control Allowed Subclasses
A class definition can specify a list of classes that it allows as subclasses. Classes not in the list cannot be defined as subclass of the class. To specify the allowed subclasses, use the AllowedSubclasses class attribute.
The AllowedSubclasses attribute provides a design point between Sealed classes, which do not allow subclassing, and the default behavior, which does not restrict subclassing.
By controlling the allowed subclasses, you can create a sealed hierarchy of classes. That is, a system of classes that enables a specific set of classes to derive from specific base classes, but that does not allow unrestricted subclassing.
See Define Sealed Hierarchy of Classes for more about this technique.
Specify Allowed Subclasses
Specify a list of one or more allowed subclasses in the
classdef statement by assigning matlab.metadata.Class objects to the
AllowedSubclasses attribute. Create the
matlab.metadata.Class object referencing a specific class
using the ? operator and the class name:
classdef (AllowedSubclasses = ?ClassName) MySuperClass ... end
Use a cell array of matlab.metadata.Class objects to define
more than one allowed subclass:
classdef (AllowedSubclasses = {?ClassName1,?ClassName2,...?ClassNameN}) MySuperClass
...
endAlways use the fully qualified class name when referencing the class name:
classdef (AllowedSubclasses = ?namespace.inneramespace.ClassName1) MySuperClass ... end
Assigning an empty cell array to the AllowedSubclasses attribute is effectively the same as defining a Sealed class.
classdef (AllowedSubclasses = {}) MySuperClass
...
endNote
Use only the ? operator and the class name to
generate matlab.metadata.Class objects. Values assigned to
the AllowedSubclasses attribute cannot contain any other
MATLAB® expressions, including functions that return either
matlab.metadata.Class objects or cell arrays of
matlab.metadata.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 also 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. Any referenced subclass that MATLAB cannot find when loading the class is effectively removed from the
list without causing an error or warning. MATLAB remembers the referenced class in case it becomes available at a
later point in time.
Note
If MATLAB does not find any of the classes in the allowed subclasses
list, the class is effectively Sealed. A sealed class is
equivalent to AllowedSubclasses = {}.
Use the matlab.metadata.Class property
RestrictsSubclassing to determine if a class is
Sealed or specifies
AllowedSubclasses.
Define Sealed Hierarchy of Classes
The AllowedSubclasses attribute enables you to define a sealed class hierarchy by sealing the allowed subclasses:
classdef (AllowedSubclasses = {?SubClass1,?SubClass2}) SuperClass ... end
Define the allowed subclasses as Sealed:
classdef (Sealed) SubClass1 ... end classdef (Sealed) SubClass2 ... end
Sealed class hierarchies enable you to use the level of abstraction that your design requires while maintaining a closed system of classes.