MATLAB Object-Oriented Programming

(Joyce) #1

Implicit Call to Inherited Constructor


MATLAB passes arguments implicitly from a default subclass constructor to the
superclass constructor. This behavior eliminates the need to implement a constructor
method for a subclass only to pass arguments to the superclass constructor.


For example, the following class constructor requires one input argument (a datetime
object), which the constructor assigns to the CurrentDate property.


classdef BaseClassWithConstr
properties
CurrentDate datetime
end
methods
function obj = BaseClassWithConstr(dt)
obj.CurrentDate = dt;
end
end
end


Suppose that you create a subclass of BaseClassWithConstr, but your subclass does
not require an explicit constructor method.


classdef SubclassDefaultConstr < BaseClassWithConstr
...
end


You can construct an object of the SubclassDefaultConstr by calling its default
constructor with the superclass argument:


obj = SubclassDefaultConstr(datetime);


For information on subclass constructors, see “Subclass Constructors” on page 9-26 and
“Default Constructor” on page 9-23.


Errors During Class Construction


For handle classes, MATLAB calls the delete method when an error occurs under the
following conditions:



  • A reference to the object is present in the code prior to the error.

  • An early return statement is present in the code before the error.


Class Constructor Methods
Free download pdf