Documentation Center

  • Trial Software
  • Product Updates

Comparing MATLAB with 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 string 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 Access 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 software uses the left-most object to select the method to call. However, if the class of that argument is superior to the other arguments, MATLAB can dispatch to a method of an argument in any position within an 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.

Modifying Objects

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.

Passing Objects 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 Comparing 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 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.

Common Object-Oriented Techniques

This table provides links to sections that discuss object-oriented techniques commonly used by other object-oriented languages.

TechniqueHow to Use in MATLAB
Operator overloadingImplementing Operators for Your Class
Multiple inheritanceSubclassing Multiple Classes
SubclassingCreating Subclasses — Syntax and Techniques
DestructorHandle Class Destructor
Data member scopingProperty Attributes
Packages (scoping classes)Packages Create Namespaces
Named constantsSee Properties with Constant Values and Defining Named Values
EnumerationsWorking with Enumerations
Static methodsStatic Methods
Static properties

Not supported. See persistent variables. For the equivalent of Java static final or C++ static const properties, use Constant properties. See Properties with Constant Values

ConstructorClass Constructor Methods
Copy constructorNo direct equivalent
Reference/reference classesComparing Handle and Value Classes
Abstract class/InterfaceDefining Abstract Classes
Garbage collectionObject Lifecycle
Instance propertiesDynamic Properties — Adding Properties to an Instance
Importing classesImporting Classes
Events and ListenersEvents and Listeners — Concepts

Was this topic helpful?