| MATLAB® | ![]() |
| On this page… |
|---|
New Class-Definition Syntax Introduced with MATLAB® Software Version 7.6 |
MATLAB® software Version 7.6 introduces a new syntax for defining classes. This new syntax includes:
The classdef keyword begins a block of class-definitions code that is terminated with an end statement.
Within the classdef code block, properties, methods, and events are also keywords delineating where you define the respective class members.
It is not possible to create class hierarchies that mix classes defined prior to Version 7.6 and current class definitions (i.e., using classdef). Therefore, you cannot subclass an old class to create a new version.
For classes defined using the new classdef keyword, an @-directory shadows all @-directories that occur after it on the MATLAB path. Classes defined in @-directories must locate all class files in that single directory. However, classes defined in @-directories continue to take precedence over functions and scripts having the same name, even those that come before them on the path.
You do not need to define private directories in class directories in Version 7.6. You can set the method's Access attribute to private instead.
Class constructor methods have two major differences:
The class function is not used.
It must call the superclass constructor only if you need to pass arguments to its constructor. Otherwise, no call to the superclass constructor is necessary.
Compare the following two Stock constructor methods. The Stock class is a subclass of the Asset class, which requires arguments passed to its constructor.
Constructor Function Prior to Version 7.6
function s = Stock(description,num_shares,share_price) s.NumShares = num_shares; s.SharePrice = share_price; % Construct Asset object a = Asset(description,'stock',share_price*num_shares); % Use the class function to define the stock object s = class(s,'Stock',a);
The same Stock class constructor is now written as shown below. The inheritance is defined on the classdef line and the constructor is defined within a methods block.
Constructor Function for Version 7.6
classdef Stock < Asset ... methods function s = Stock(description,num_shares,share_price) % Call superclass constructor to pass arguments s = s@Asset(description,'stock',share_price*num_shares); s.NumShares = num_shares; s.SharePrice = share_price; end % End of function end % End of methods block end % End of classdef block
Properties: How to Use Properties
Handle classes: Comparing Handle and Value Classes
Events and listeners: Events and Listeners — Concepts
Class member attributes: Attribute Tables
Abstract classes: Abstract Classes and Interfaces
Dynamic properties: Dynamic Properties — Adding Properties to an Instance
Ability to subclass MATLAB built-in classes: Creating Subclasses — Syntax and Techniques
Packages for scoping functions and classes: Scoping Classes with Packages. Packages are not supported for classes created prior to MATLAB Version 7.6 (i.e., classes that do not use classdef).
The MATLAB Version 7.6 implementation of classes uses significantly different syntax from previous releases. However, classes written in previous versions continue to work in this and future versions. Most of the code you use to implement the methods is likely to remain the same, except where you take advantage of new features.
The following sections re-implement examples using the latest syntax. These same classes were implemented in the original MATLAB Classes and Objects documentation and provide a means for comparison.
Example — A Simple Class Hierarchy
Example — Containing Assets in a Portfolio
Documentation for MATLAB Classes and Objects prior to Version 7.6 is available here.
![]() | Using the Editor and Debugger with Classes | MATLAB® and Other OO Languages | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |