| MATLAB® | ![]() |
| On this page… |
|---|
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.
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.
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);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.
In C++, you call a superclass method using the scoping operator: superclass::method
In Java code, you use: superclass.method
The equivalent MATLAB operation is method@superclass.
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.
This table provides links to sections where object-oriented techniques commonly used by other object-oriented languages are discussed.
| Technique | How to Use in MATLAB |
|---|---|
| Operator overloading | Overloading Operators |
| Multiple inheritance | Using Multiple Inheritance |
| Subclassing | Creating Subclasses — Syntax and Techniques |
| Destructor | Handle Class Delete Methods |
| Data member scoping | Property Attributes |
| Packages (scoping classes) | Scoping Classes with Packages |
| Named constants | Defining Named Constants |
| Static members | Static Methods |
| Constructor | Class Constructor Methods |
| Copy constructor | No direct equivalent |
| Reference/reference classes | Comparing Handle and Value Classes |
| Abstract class/Interface | Abstract Classes and Interfaces |
| Garbage collection | Object Lifecycle |
| Instance properties | Dynamic Properties — Adding Properties to an Instance |
| Importing classes | Importing Classes |
| Events and Listeners | Events and Listeners — Concepts |
![]() | Compatibility with Previous Versions | Example — Representing Structured Data | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |