Products & Services Industries Academia Support User Community Company

Learn more about MATLAB   

Creating Subclasses — Syntax and Techniques

Defining a Subclass

To define a class that is a subclass of another class, add the superclass to the classdef line after a < character:

classdef classname < superclassname

When inheriting from multiple classes, use the & character to indicate the combination of the superclasses:

classdef classname < super1 & super2

See Class Member Compatibility for more information on deriving from multiple superclasses.

Class Attributes

Subclasses do not inherit superclass attributes.

Referencing Superclasses from Subclasses

When you construct an instance of a subclass, you must use the obj@baseclass1(args) syntax to initialize the object for each superclass. For example, the following segment of a class definition shows a class called stock that is a subclass of a class called asset.

classdef stock < asset
   methods
      function s = stock(asset_args,...)
         if nargin == 0
            ...
         end
         s = s@asset(asset_args) % call asset constructor
         ...
      end
   end
end

Constructing Subclasses provides more information on creating subclass constructor methods.

Referencing Superclasses Contained in Packages

If you are deriving a class from a superclass that is contained in a package and you want to initialize the object for the superclass, you must include the package name. For example:

classdef stock < financial.asset
   methods
      function s = stock(asset_args,...)
         if nargin == 0
            ...
         end
         s = s@financial.asset(asset_args) % call asset constructor
         ...
      end
   end
end

Initializing Objects When Using Multiple Superclasses

If you are deriving a class from multiple superclasses, initialized the subclass object with calls to each superclass constructor:

classdef stock < financial.asset & trust.member
   methods
      function s = stock(asset_args,member_args,...)
         if nargin == 0
            ...
         end
         s = s@financial.asset(asset_args) % call asset constructor
         s = s@trust.member(member_args) % call member constructor
         ...
      end
   end
end

Constructor Arguments and Object Initialization

You cannot conditionalize the initialization of the object by superclasses. However, you should always ensure that your class constructor can be called with zero arguments.

You can satisfy the need for a zero-argument syntax by assigning appropriate values to input argument variables before constructing the object:

classdef stock < financial.asset
   properties
      SharePrice
   end
   methods
      function s = stock(name,pps)
         if nargin == 0
            name = '';
            pps = [];
         end
         s = s@financial.asset(name) % call superclass constructor
         s.SharePrice = pps;  % assign a property value
      end
   end
end

See Supporting the No Input Argument Case.

Call Only Direct Superclass from Constructor

You cannot call an indirect superclass constructor from a subclass constructor. For example, suppose class B is derived from class A and class C is derived from class B. The constructor for class C should not call the constructor for class A to initialize properties. The call to initialize class A properties should be made from class B.

The following implementations of classes A, B, and C show how to design this relationship among the classes.

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 = 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 = obj@B(x,y)
      end
   end
end

Creating an Alias for an Existing Class

You can refer to a class using a different name by creating an alias for that class. This technique is similar to the C++ typedef concept. To create an alias, create an empty subclass:

classdef newclassname < oldclassname
end

The old class constructor must be callable with zero input arguments. If not, see Old Class Constructor Requires Arguments.

This technique is useful when reloading objects that were saved using the old class name. However, the class of the object reflects the new name. For example,

class(obj) 

returns the new class name.

Old Class Constructor Requires Arguments

If the old class constructor requires arguments, add a constructor to the new class:

classdef NewClasss < OldClass
   methods
      function obj = NewClass(x,y)
         obj = obj@OldClass(x,y)
      end
end
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS