Main Content

Design Subclass Constructors

Call Superclass Constructor Explicitly

Explicitly calling each superclass constructor from a subclass constructor enables you to:

  • Pass arguments to superclass constructors

  • Control the order in which MATLAB® calls the superclass constructors

If you do not explicitly call the superclass constructors from the subclass constructor, MATLAB implicitly calls these constructors with no arguments. The superclass constructors must support the no argument syntax to support implicit calls, and the constructors are called in the order they appear at the top of the class block, from left to right. To change the order in which MATLAB calls the constructors or to call constructors with arguments, call the superclass constructors explicitly from the subclass constructor.

If you do not define a subclass constructor, you can call the default constructor with superclass arguments. For more information, see Default Constructor and Implicit Call to Inherited Constructor.

Call Superclass Constructor from Subclass

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

obj@SuperClass1(args,...);

...

obj@SuperclassN(args,...);

Where obj is the output of the subclass 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 Stocks that is a subclass of a class called Assets.

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

Subclass Constructors provides more information on creating subclass constructor methods.

Reference Superclasses Contained in Namespaces

If a superclass is contained in a namespace, include the namespace name. For example, the Assests class is in the finance namespace:

classdef Stocks < finance.Assets
   methods
      function s = Stocks(asset_args,...)
         if nargin == 0
            ...
         end
         % Call asset constructor
         s@finance.Assets(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 Stocks < finance.Assets & Taxable
   methods
      function s = Stocks(asset_args,tax_args,...)
         if nargin == 0
            ...
         end
         % Call asset and member class constructors
         s@finance.Assets(asset_args)
         s@Taxable(tax_args)
         ...
      end
   end
end

Subclass Constructor Implementation

To ensure that your class constructor supports the zero arguments syntax, assign default values to input argument variables before calling the superclass constructor. You cannot conditionalize a subclass call to the superclass constructor. Locate calls to superclass constructors outside any conditional code blocks.

For example, the Stocks class constructor supports the no argument case with the if statement, but calls the superclass constructor outside of the if code block.

classdef Stocks < finance.Assets
   properties
      NumShares
      Symbol 
   end
   methods
      function s = Stocks(description,numshares,symbol)
         if nargin == 0
            description = '';
            numshares = 0;
            symbol = '';
         end
         s@finance.Assets(description);
         s.NumShares = numshares;
         s.Symbol = symbol;
      end
   end
end

Call Only Direct Superclass from Constructor

Call only direct superclass constructors 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

Related Topics