MATLAB Object-Oriented Programming

(Joyce) #1

Overload Functions in Class Definitions


In this section...
“Why Overload Functions” on page 9-34
“Implementing Overloaded MATLAB Functions” on page 9-34
“Rules for Naming to Avoid Conflicts” on page 9-36

Why Overload Functions


Classes can redefine MATLAB functions by implementing methods having the same name.
Overloading is useful when defining specialized types that you want to behave like
existing MATLAB types. For example, you can implement relational operations, plotting
functions, and other commonly used MATLAB functions to work with objects of your class.

You can also modify default behaviors by implementing specific functions that control
these behaviors. For more information on functions that modify default behaviors, see
“Methods That Modify Default Behavior” on page 17-2.

Implementing Overloaded MATLAB Functions


Class methods can provide implementations of MATLAB functions that operate only on
instances of the class. This restriction is possible because MATLAB can always identify to
which class an object belongs.

MATLAB uses the dominant argument to determine which version of a function to call. If
the dominant argument is an object, then MATLAB calls the method defined by the
object's class, if one exists.

In cases where a class defines a method with the same name as a global function, the
class's implementation of the function is said to overload the original global
implementation.

To overload a MATLAB function:


  • Define a method with the same name as the function you want to overload.

  • Ensure that the method argument list accepts an object of the class, which MATLAB
    uses to determine which version to call.


9 Methods — Defining Class Operations

Free download pdf