Documentation

Create Subclasses — Syntax and Techniques

Subclass Definition

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.

Initialize Superclasses from Subclasses

To initialize the object for each superclass within the subclass constructor, use the following syntax:

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 that 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.

Reference 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

Initialize Objects 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

Call Superclass Constructor Explicitly

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.

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, Call the superclass constructors explicitly from the subclass constructor.

Constructor Arguments and Object Initialization

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

Ensure that your class constructor supports the zero arguments syntax. To satisfy the need for a zero-argument syntax, assign 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

Call Only Direct Superclass from Constructor

You can call only a direct 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 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

Subclass Alias for 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.

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. That is, this code returns the new class name.

class(obj) 

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
end

Related Examples

Was this topic helpful?