Comparison of MATLAB and Other OO Languages
Some Differences from C++ and Java Code
The MATLAB® programming language differs from other object-oriented languages, such as C++ or Java® 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 assigning or querying property values. For example, the following statement:
myobj.Material = 'plastic';
assigns the char
vector plastic
to the
Material
property of myobj
. Before making
the actual assignment, myobj
executes a method called
set.Material
(assuming the class of myobj
defines this method), which can perform any necessary operations. See Property Get and Set Methods 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.
No Implicit Parameters
In some languages, one object parameter to a method is always implicit. In MATLAB, objects are explicit parameters to the methods that act on them.
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 uses the leftmost object to select the method to call.
However, if the class of an argument is superior to the class of the other arguments, MATLAB dispatches to the method of the superior argument, regardless of its position within the argument list.
See Class Precedence for more information.
Calling Superclass Method
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
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.
Object Modification
MATLAB classes can define public properties, which you can modify by explicitly assigning values to those properties on a given instance of the class. However, only classes derived from the handle
class exhibit reference behavior. Modifying a property value on an instance of a value classes (classes not derived from handle
), changes the value only within the context in which the modification is made.
The sections that follow describe this behavior in more detail.
Objects Passed to Functions
MATLAB passes all variables by value. When you pass an object to a function, MATLAB copies the value from the caller into the parameter variable in the called function.
However, MATLAB supports two kinds of classes that behave differently when copied:
Handle classes — a handle class instance variable refers to an object. A copy of a handle class instance variable refers to the same object as the original variable. If a function modifies a handle object passed as an input argument, the modification affects the object referenced by both the original and copied handles.
Value classes — the property data in an instance of a value class are independent of the property data in copies of that instance (although, a value class property could contain a handle). A function can modify a value object that is passed as an input argument, but this modification does not affect the original object.
See Comparison of Handle and Value Classes for more information on the behavior and use of both kinds of classes.
Passing Value Objects. When you pass a value object to a function, the function creates a local copy of the argument variable. The function can modify only the copy. If you want to modify the original object, return the modified object and assign it to the original variable name. For example, consider the value class, SimpleClass
:
classdef SimpleClass properties Color end methods function obj = SimpleClass(c) if nargin > 0 obj.Color = c; end end end end
Create an instance of SimpleClass
, assigning a value of red
to its Color
property:
obj = SimpleClass('red');
Pass the object to the function g
, which assigns blue
to the Color
property:
function y = g(x) x.Color = 'blue'; y = x; end
y = g(obj);
The function g
modifies its copy of the input object and returns that copy, but does not change the original object.
y.Color
ans = blue
obj.Color
ans = red
If the function g
did not return a value, the modification of the object Color
property would have occurred only on the copy of obj
within the function workspace. This copy would have gone out of scope when the function execution ended.
Overwriting the original variable actually replaces it with a new object:
obj = g(obj);
Passing Handle Objects. When you pass a handle to a function, the function makes a copy of the handle variable, just like when passing a value object. However, because a copy of a handle object refers to the same object as the original handle, the function can modify the object without having to return the modified object.
For example, suppose that you modify the SimpleClass
class definition to make a class derived from the handle
class:
classdef SimpleHandleClass < handle properties Color end methods function obj = SimpleHandleClass(c) if nargin > 0 obj.Color = c; end end end end
Create an instance of SimpleHandleClass
, assigning a value of red
to its Color
property:
obj = SimpleHandleClass('red');
Pass the object to the function g
, which assigns blue
to the Color
property:
y = g(obj);
The function g
sets the Color
property of the object referred to by both the returned handle and the original handle:
y.Color
ans = blue
obj.Color
ans = blue
The variables y
and obj
refer to the same object:
y.Color = 'yellow';
obj.Color
ans = yellow
The function g
modified the object referred to by the input argument (obj
) and returned a handle to that object in y
.
MATLAB Passes Handles by Value. A handle variable is a reference to an object. MATLAB passes this reference by value.
Handles do not behave like references in C++. If you pass an object handle to a function and that function assigns a different object to that handle variable, the variable in the caller is not affected. For example, suppose you define a function g2
:
function y = g2(x) x = SimpleHandleClass('green'); y = x; end
Pass a handle object to g2
:
obj = SimpleHandleClass('red');
y = g2(obj);
y.Color
ans = green
obj.Color
ans = red
The function overwrites the handle passed in as an argument, but does not overwrite the object referred to by the handle. The original handle obj
still references the original object.
Static Properties
In MATLAB, classes can define constant properties, but not "static" properties in the sense of other languages like C++. You cannot change constant properties from the initial value specified in the class definition.
MATLAB has long-standing rules that variables always take precedence over the names of functions and classes. Assignment statements introduce a variable if one does not exist.
Expressions of this form
A.B = C
Introduce a new variable, A
, that is a struct
containing a field B
whose value is C
. If A.B = C
could refer to a static property of class A
, then class A
would take precedence over variable A
.
This behavior would be a significant incompatibility with prior releases of MATLAB. For example, the introduction of a class named A
on the MATLAB path could change the meaning of an assignment statement like A.B = C
inside a .m
code file.
In other languages, classes rarely use static data, except as private data within the class or as public constants. In MATLAB, you can use constant properties the same way you use public
final
static
fields in Java. To use data that is internal to a class in MATLAB, create persistent variables in private or protected methods or local functions used privately by the class.
Avoid static data in MATLAB. If a class has static data, using the same class in multiple applications causes conflicts among applications. Conflicts are less of an issue in some other languages. These languages compile applications into executables that run in different processes. Each process has its own copy of the class static data. MATLAB, frequently runs many different applications in the same process and environment with a single copy of each class.
For ways to define and use static data in MATLAB, see Static Data.
Common Object-Oriented Techniques
This table provides links to sections that discuss object-oriented techniques commonly used by other object-oriented languages.
Technique | How to Use in MATLAB |
---|---|
Operator overloading | Operator Overloading |
Multiple inheritance | Subclassing Multiple Classes |
Subclassing | Design Subclass Constructors |
Destructor | Handle Class Destructor |
Data member scoping | Property Attributes |
Namespaces (scoping classes) | Create Namespaces |
Named constants | See Define Class Properties with Constant Values and Named Values |
Enumerations | Define Enumeration Classes |
Static methods | Static Methods |
Static properties | Not supported. See For mutable static data, see Static Data |
Constructor | Class Constructor Methods |
Copy constructor | No direct equivalent |
Reference/reference classes | Comparison of Handle and Value Classes |
Abstract class/Interface | Abstract Classes and Class Members |
Garbage collection | Object Lifecycle |
Instance properties | Dynamic Properties — Adding Properties to an Instance |
Importing classes | Import Namespace Members into Functions |
Events and Listeners | Event and Listener Concepts |