| MATLAB® | ![]() |
| On this page… |
|---|
Referencing Superclasses from Subclasses |
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.
Subclasses do not inherit superclass attributes, with the exception of the InferiorClasses attribute. If both superclasses and the subclass define the InferiorClasses attribute, the true list of inferior classes is the union of the inferior classes defined by the subclass and all superclasses.
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.
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
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
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.
You can create an alias for a class that enables you to refer to the class using a different name. This technique is similar to the C++ typedef concept. To create an alias, create an empty subclass:
classdef newclassname < oldclassname end
This technique might be 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.
![]() | Hierarchies of Classes — Concepts | Modifying Superclass Methods and Properties | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |