MATLAB Object-Oriented Programming

(Joyce) #1

Design Subclass Constructors


In this section...
“Call Superclass Constructor Explicitly” on page 12-9
“Call Superclass Constructor from Subclass” on page 12-9
“Subclass Constructor Implementation” on page 12-11
“Call Only Direct Superclass from Constructor” on page 12-12

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.

MATLAB does not guarantee any specific calling order when there are multiple
superclasses. If the order in which MATLAB calls the superclass constructors is
important, 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” on page 9-23 and
“Implicit Call to Inherited Constructor” on page 9-29.

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,...);

Design Subclass Constructors
Free download pdf