Controlling Allowed Subclasses

Basic Knowledge

The material presented in this section builds on an understanding of the following information:

Why Control Allowed Subclasses

A class definition can specify a list of classes that it allows to subclass the class. Classes not in the list cannot subclass the class. Use the AllowedSubclasses class attribute to specify the allowed subclasses.

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 a Sealed Hierarchy of Classes for more about this technique.

Specify Allowed Subclasses

    Note:   Specify attribute values explicitly, not as values returned from functions or other MATLAB® expressions.

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

Use a cell array of meta.class objects to define more than one allowed subclass:

classdef (AllowedSubclasses = {?ClassName1,?ClassName2,...?ClassNameN}) MySuperClass

Always use the fully qualified class name when referencing the class name:

classdef (AllowedSubclasses = ?Package.SubPackage.ClassName1) MySuperClass

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

classdef (AllowedSubclasses = {}) MySuperClass

    Note:   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.

Effect of Defining a Class as an Allowed Subclass

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.

    Note:   If MATLAB does not find any of the classes in the allowed classes list, the class is effectively Sealed. This is equivalent to AllowedSubclasses = {}.

Define a 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

Define the allowed subclasses as Sealed:

classdef (Sealed) SubClass1

classdef (Sealed) SubClass2

Sealed class hierarchies enable you to use the level of abstraction that your design requires while maintaining a closed systems of classes.

See Supporting Both Handle and Value Subclasses for related information.

Was this topic helpful?