Compatibility with Previous Versions

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:

Cannot Mix Class Hierarchy

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.

Only One @-Directory per Class

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.

Private Methods

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.

Changes to Class Constructors

Class constructor methods have two major differences:

Example of Old and New Syntax

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

New Features Introduced with Version 7.6

Examples of Old and New

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 Polynomial Class

Example — A Simple Class Hierarchy

Example — Containing Assets in a Portfolio

Obsolete Documentation

Documentation for MATLAB Classes and Objects prior to Version 7.6 is available here.

  


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