Creating Subclasses — Syntax and Techniques

Defining a Subclass

To define a class that is a subclass of another class, add the superclass to the classdef line after a < character:

classdef ClassName < SuperClass

When inheriting from multiple classes, use the & character to indicate the combination of the superclasses:

classdef ClassName < SuperClass1 & SuperClass2

See Class Member Compatibility for more information on deriving from multiple superclasses.

Class Attributes

Subclasses do not inherit superclass attributes.

Initializing Superclasses from Subclasses

Use the following syntax to initialize the object for each superclass within the subclass constructor.

obj@SuperClass1(args,...);

...

obj@SuperclassN(args,...);

Where obj is the output of the constructor, SuperClass... is the name of a superclass, and args are any arguments required by the respective superclass constructor.

For example, the following segment of a class definition shows a class called stock that is a subclass of a class called asset.

classdef stock < asset
   methods
      function s = stock(asset_args,...)
         if nargin == 0
            ... % Assign values to asset_args
         end
         % Call asset constructor
         s@asset(asset_args); 
         ...
      end
   end
end

Constructing Subclasses provides more information on creating subclass constructor methods.

Referencing Superclasses Contained in Packages

If a superclass is contained in a package, include the package name. For example:

classdef stock < financial.asset
   methods
      function s = stock(asset_args,...)
         if nargin == 0
            ...
         end
         % Call asset constructor
         s@financial.asset(asset_args); 
         ...
      end
   end
end

Initializing Objects When Using Multiple Superclasses

To derive a class from multiple superclasses, initialize the subclass object with calls to each superclass constructor:

classdef stock < financial.asset & trust.member
   methods
      function s = stock(asset_args,member_args,...)
         if nargin == 0
            ...
         end
         % Call asset and member class constructors
         s@financial.asset(asset_args)
         s@trust.member(member_args)
         ...
      end
   end
end

Calling Superclass Constructor Explicity

Explicitly calling each superclass constructor enables you to:

  • Pass arguments to superclass constructors

  • Control the order in which the superclass constructors are called

If you do not explicitly call the superclass constructors from the subclass constructor, MATLAB® implicitly calls these constructors with no arguments. In this case, the superclass constructors must support no argument syntax. See No Input Argument Constructor Requirement for more information.

In the case of multiple superclasses, MATLAB does not guarantee any specific calling sequence. If the order in which MATLAB calls the superclass constructors is important, you must explicitly call the superclass constructors from the subclass constructor.

Constructor Arguments and Object Initialization

You cannot conditionalize calls to the superclass initialization of the object. Locate calls to superclass constructors outside any conditional code blocks.

However, ensure that your class constructor supports the zero arguments syntax. You can satisfy the need for a zero-argument syntax by assigning appropriate values to input argument variables before constructing the object:

For example, the stock class constructor supports the no argument case with the if statement, but initializes the object for the superclass outside of the if code block.

classdef stock < financial.asset
   properties
      SharePrice
   end
   methods
      function s = stock(name,pps)
         % Support no input argument case
         if nargin == 0
            name = '';
            pps = 0;
         end
         % Call superclass constructor
         s@financial.asset(name)
         % Assign property value
         s.SharePrice = pps;
      end
   end
end

See No Input Argument Constructor Requirement.

Call Only Direct Superclass from Constructor

You cannot call an indirect superclass constructor from a subclass constructor. For example, suppose class B derives from class A and class C derives from class B. The constructor for class C cannot call the constructor for class A to initialize properties. Class B must make the call to initialize class A properties.

The following implementations of classes A, B, and C show how to design this relationship in each class.

Class A defines properties x and y, but assigns a value only to x:

classdef A
   properties 
      x
      y
   end
   methods
      function obj = A(x)
         ...
         obj.x = x;
      end
   end
end

Class B inherits properties x and y from class A. The class B constructor calls the class A constructor to initialize x and then assigns a value to y.

classdef B < A
   methods
      function obj = B(x,y)
         ...
         obj@A(x);
         obj.y = y;
      end
   end
end

Class C accepts values for the properties x and y, and passes these values to the class B constructor, which in turn calls the class A constructor:

classdef C < B
   methods
      function obj = C(x,y)
         ...
         obj@B(x,y);
      end
   end
end

Sequence of Constructor Calls in a Class Hierarchy

MATLAB does not guarantee the sequence in which superclass constructors are called when constructing a subclass object. However, you can control the order in which class constructors are called by calling superclass constructors explicitly from the subclass constructor.

If you explicitly call a superclass constructor from the most specific subclass constructor, then MATLAB calls the most specific subclass constructor first. If you do not make an explicit call to a superclass constructor from the subclass constructor, MATLAB makes the implicit call before accessing the object.

Suppose you have a hierarchy of classes in which ClassC derives from ClassB, which derives from ClassA. The constructor for a subclass can call only direct superclasses. Therefore, each class constructor can call the direct superclass constructor:

In cases of multiple inheritance, the subclass constructor can call each superclass constructor. To ensure a specific superclass constructor calling sequence is followed, your most specific subclass constructor must explicitly call ALL superclass constructors:

If you do not explicitly call all direct superclass constructors, MATLAB does not guarantee the order in which the superclass constructors are called.

Using a Subclass to Create an Alias for an Existing Class

You can refer to a class using a different name by creating an alias for that class. This technique is like the C++ typedef concept. To create an alias, create an empty subclass:

classdef NewClassName < OldClassName
end

The old class constructor must be callable with zero input arguments. If not, see Old Class Constructor Requires Arguments.

This technique is useful when reloading objects that you saved using the old class name. However, the class of the object reflects the new name. For example,

class(obj) 

returns the new class name.

Old Class Constructor Requires Arguments

If the old class constructor requires arguments, add a constructor to the new class:

classdef NewClass < OldClass
   methods
      function obj = NewClass(x,y)
         obj@OldClass(x,y);
      end
end
Was this topic helpful?