Documentation Center

  • Trial Software
  • Product Updates

Overloading Functions for Your Class

Overloading MATLAB Functions

Class methods can provide implementations of MATLAB® functions that operate only on instances of the class. This is possible because MATLAB software 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 there is one.

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

    Note:   MATLAB does not support overloading functions using different signatures for the same function name.

For additional information on overloading functions, see:

Implementing MATLAB Operators

Classes designed to implement new MATLAB data types typically define certain operators, such as addition, subtraction, equality, and so on.

For example, standard MATLAB addition (+) cannot add two polynomials because this operation is not defined by simple addition. However, a polynomial class can define its own plus method that the MATLAB language calls to perform addition of polynomial objects when you use the + symbol:

p1 + p2

Implementing Operators for Your Class provides information on methods to overload.

Defining Arithmetic Operators for DocPolynom provides examples.

Rules for Naming to Avoid Conflicts

The names of methods, properties, and events are scoped to the class. Therefore, you should adhere to the following rules to avoid naming conflicts:

  • You can reuse names that you have used in unrelated classes.

  • You can reuse names in subclasses if the member does not have public or protected access. These names then refer to entirely different methods, properties, and events without affecting the superclass definitions

  • Within a class, all names exist in the same name space and must be unique. A class cannot define two methods with the same name and a class cannot define a local function with the same name as a method.

  • The name of a static method is considered without its class prefix. Thus, a static method name without its class prefix cannot match the name of any other method.

Was this topic helpful?