MATLAB Object-Oriented Programming

(Joyce) #1

Class Precedence


In this section...
“Use of Class Precedence” on page 6-23
“Why Mark Classes as Inferior” on page 6-23
“InferiorClasses Attribute” on page 6-23

Use of Class Precedence


MATLAB uses class precedence to determine which method to call when multiple classes
have the same method. You can specifying the relative precedence of user-defined classes
with the class InferiorClasses attribute.

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


  • “Class Metadata” on page 16-2

  • “Attribute Specification” on page 5-22


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:

(^1) Determine the dominant argument based on the class of arguments.
(^2) If there is a dominant argument, call the method of the dominant class.
(^3) If arguments are of equal precedence, use the leftmost argument as the dominant
argument.
(^4) 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
Specify the relative precedence of user-defined classes using the class
InferiorClasses attribute. To specify classes that are inferior to the class you are
defining, assign a cell array of class meta.class objects to this attribute.
Class Precedence

Free download pdf