The material presented in this section builds on an understanding of the following information:
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 a list of one or more allowed subclasses in the classdef
statement
by assigning meta.class
objects
to the AllowedSubclasses
attribute. Create the meta.class
object
referencing a specific class using the ?
operator and the class name:
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
Use 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.
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.
If 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 = {}
.
Use the meta.class
property RestrictsSubclassing
to
determine if a class is Sealed
or specifies AllowedSubclasses
.
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.