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:

  • The classdef keyword begins a block of class-definitions code. An end statement terminates the class definition.

  • Within the classdef code block, properties, methods, and events are also keywords delineating where you define the respective class members.

Cannot Mix Class Hierarchy

It is not possible to create class hierarchies that mix classes defined before Version 7.6 and current class definitions that use classdef. Therefore, you cannot subclass an old class to create a version of the new class.

Only One “@” Class Folder Per Class

For classes defined using the new classdef keyword, a class folder shadows all class folders that occur after it on the MATLAB path. Classes defined in class folders must locate all class files in that single folder. However, classes defined in class folders continue to take precedence over functions and scripts having the same name, even those functions and scripts that come before them on the path.

Private Methods

You do not need to define private folders in class folders 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. Class constructors:

  • Do not use the class function.

  • Must call the superclass constructor only if you want to pass arguments to its constructor. Otherwise, no call to the superclass constructor is necessary.

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 Before 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);

Write the same Stock class constructor as shown here. Define the inheritance on the classdef line and define the constructor 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 different syntax from previous releases. However, classes written in previous versions continue to work. 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 reimplement examples using the latest syntax. The original MATLAB Classes and Objects documentation implemented these same examples and provide a comparison of old and new syntax.

Representing Polynomials with Classes

A Class Hierarchy for Heterogeneous Arrays