MATLAB Object-Oriented Programming

(Joyce) #1

Specify Allowed Subclasses


In this section...
“Basic Knowledge” on page 12-25
“Why Control Allowed Subclasses” on page 12-25
“Specify Allowed Subclasses” on page 12-25
“Define Sealed Hierarchy of Classes” on page 12-27

Basic Knowledge


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


  • “Class Metadata” on page 16-2

  • “Attribute Specification” on page 5-22


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” on page 12-27 for more about this technique.

Specify Allowed Subclasses


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:

Specify Allowed Subclasses
Free download pdf