MATLAB® and Other OO Languages

Some Differences from C++ and Sun™ Java™ Code

If you are accustomed to programming in other object-oriented languages, such as C++ or the Java™ language, you will find that the MATLAB® programming language differs from these languages in some important ways.

Public Properties

Unlike fields in C++ or the Java language, you can use MATLAB properties to define a public interface separate from the implementation of data storage. You can provide public access to properties because you can define set and get access methods that execute automatically when property values are assigned or queried by code external to object methods. For example, the following statement:

myobj.Material = 'plastic';

assigns the string plastic to the Material property of myobj. However, before making the actual assignment, myobj executes a method called set.Material (assuming the class of myobj defines this method) which can perform any operations on the data to check its validity, set other values, and so on. See Controlling Property Access for more information on property access methods.

You can also control access to properties by setting attributes, which enable public, protected , or private access. See Property Attributes for a full list of property attributes.

Pass By Reference

In MATLAB classes, variables are not passed by reference. However, MATLAB classes support two kinds of classes that behave in different ways with respect to referenced data: value classes and handle classes.

Value Classes.   When writing methods that update an object, you must pass back the updated object and use an assignment statement. For example, a value class implementation of a set method for a property requires the object to be returned with the new value set.

A = set(A,'PropertyName',PropertyValue);

Handle Classes.   If A is a handle object, then there is only one copy of the data so setting the value of a property changes the data that is referred to by the handle variable. For example, a handle class behaves like Handle Graphics® objects:

set(A,'PropertyName',PropertyValue);

Dispatching

In MATLAB classes, method dispatching is not based on method signature, as it is in C++ and Java code. When the argument list contains objects of equal precedence, MATLAB software uses the left-most object to select the method to call. However, MATLAB can dispatch to a method of an argument in any position within an argument list if the class of that argument is superior to the other arguments.

See Specifying Class Precedence for more information.

Calling Superclass Method

The equivalent MATLAB operation is method@superclass.

Other Differences

In MATLAB classes, there is no equivalent to C++ templates or Java generics. However, MATLAB is weakly typed and it is possible to write functions and classes that work with different types of data.

MATLAB classes do not support overloading functions using different signatures for the same function name.

Common Object-Oriented Techniques

This table provides links to sections where object-oriented techniques commonly used by other object-oriented languages are discussed.

TechniqueHow to Use in MATLAB
Operator overloadingOverloading Operators
Multiple inheritanceUsing Multiple Inheritance
SubclassingCreating Subclasses — Syntax and Techniques
DestructorHandle Class Delete Methods
Data member scopingProperty Attributes
Packages (scoping classes)Scoping Classes with Packages
Named constantsDefining Named Constants
Static membersStatic Methods
ConstructorClass Constructor Methods
Copy constructorNo direct equivalent
Reference/reference classesComparing Handle and Value Classes
Abstract class/InterfaceAbstract Classes and Interfaces
Garbage collectionObject Lifecycle
Instance propertiesDynamic Properties — Adding Properties to an Instance
Importing classesImporting Classes
Events and ListenersEvents and Listeners — Concepts

  


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