Class Precedence

Basic Knowledge

The material presented in this section builds on an understanding of the following information:

Why Mark Classes as Inferior

When more than one class defines methods with the same name or when classes overload functions, MATLAB® determines which method or function to call based on the dominant argument. Here is how MATLAB determines the dominant argument:

  • Determine the dominant argument based on the class of arguments.

  • If there is a dominant argument, call the method of the dominant class.

  • If arguments are of equal precedence, use the left-most argument as the dominant argument.

  • If the class of the dominant argument does not define a method with the name of the called function, call the first function on the path with that name.

InferiorClasses Attribute

You can specify the relative precedence of user-defined classes using the class InferiorClasses attribute. Assign a cell array of class names (represented as meta.class objects) to this attribute to specify classes that are inferior to the class you are defining. For example, the following classdef declares that myClass is dominant over class1 and class2.

classdef (InferiorClasses = {?class1,?class2}) myClass

The ? operator combined with a class name creates a meta.class object. This syntax enables you to create a meta.class object without requiring you to construct an actual instance of the class.

MATLAB built-in classes are always inferior to user-defined classes and should not be used in this list.

The built-in classes include: double, single, char, logical, int64, uint64, int32, uint32, int16, uint16, int8, uint8, cell, struct, and function_handle.

Dominant Class

MATLAB uses class dominance when evaluating expressions involving objects of more than one class. The dominant class determines:

  • The methods of which class MATLAB calls when more than one class defines methods with the same names.

  • The class of arrays that are formed by combining objects of different classes, assuming MATLAB can convert the inferior objects to the dominant class.

See Concatenating Objects of Different Classes for more information on creating object arrays.

More Information

See Determining Which Method Is Invoked for more on how the MATLAB classes dispatch when evaluating expressions containing objects.

See Class Precedence and MATLAB Path for information on how the location of a class definition on the MATLAB path determines its precedence.

See Class Metadata for information on meta-class objects.

See Dominant Argument in Overloaded Plotting Functions for an example of class precedence.

No Attribute Inheritance

Subclasses do not inherit a superclass InferiorClasses attribute. Only instances of the classes specified in the subclass InferiorClasses attribute are inferior to subclass objects.

Was this topic helpful?