A constructor method is a special function that creates an instance of the class. Typically, constructor methods accept input arguments to assign the data stored in properties and always return an initialized object.

The constructor has the same name as the class.

The only output argument from a constructor is the object constructed.

The constructor can return only a single argument.

Constructors must always return a valid instance of the class. Never return an empty object from a class constructor.

If the class being created is a subclass, MATLAB calls the constructor of each superclass class to initialize the object. Implicit calls to the superclass constructor are made with no arguments. If superclass constructors require arguments, you must call them from the subclass constructor explicitly.

If your constructor makes an explicit call to a superclass constructor, this call must occur before any other reference to the constructed object.

A class does not need to define a constructor method unless it is a subclass of a superclass whose constructor requires arguments. In this case, you must explicitly call the superclass constructor with the required arguments. See Constructing Subclasses

If a class does not define a constructor, MATLAB supplies a constructor that takes no arguments and returns a scalar object whose properties are initialized to empty or the values specified as defaults in the property definitions. The constructor supplied by MATLAB also calls all superclass constructors with no arguments.

If you create a class constructor, you should implement class constructors so that they can be called with no input arguments, in addition to whatever arguments are normally required See Supporting the No Input Argument Case and Basic Structure of Constructor Methods.

Constructors must always return objects of their own class. A superclass constructor cannot return an object of a subclass.

Calls to superclass constructors cannot be conditional. This means superclass construction calls cannot be placed in loops, conditions, switches, try/catch, or nested functions.

You can restrict access to constructors using method attributes, as with any method.
Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates