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.
To call the constructor for each superclass within the subclass constructor, use the following syntax:
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
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.
If a superclass is contained in a package, include the package name. For example, the
Assests class is in the
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
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
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 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
The following implementations of classes
C show how to design this relationship in each class.
A defines properties
y, but assigns a value only to
classdef A properties x y end methods function obj = A(x) ... obj.x = x; end end end
B inherits properties
y from class
A. The class
B constructor calls the class
A constructor to initialize
x and then assigns a value to
classdef B < A methods function obj = B(x,y) ... obj@A(x); obj.y = y; end end end
C accepts values for the properties
y, and passes these values to the class
B constructor, which in turn calls the class
classdef C < B methods function obj = C(x,y) ... obj@B(x,y); end end end