MATLAB Object-Oriented Programming

(Joyce) #1
For example, suppose classA defines classB as inferior and suppose that both classes
define a method called combine.

Calling the method with an object of classB and classA:

combine(B,A)

actually calls the combine method of classA because A is the dominant argument.

Dot Notation vs. Function Notation

MATLAB classes support both function and dot notation syntax for calling methods. For
example, if setColor is a method of the class of object X, then calling setColor with
function notation would be:

X = setColor(X,'red');

The equivalent method call using dot notation is:

X = X.setColor('red')

However, in certain cases, the results for dot notation can differ with respect to how
MATLAB dispatching works:


  • If there is an overloaded subsref, it is invoked whenever using dot notation. That is,
    the statement is first tested to see if it is subscripted assignment.

  • If there is no overloaded subsref, then setColor must be a method of X. An
    ordinary function or a class constructor is never called using this notation.

  • Only the argument X (to the left of the dot) is used for dispatching. No other
    arguments, even if dominant, are considered. Therefore dot notation can call only
    methods of X; methods of other argument are never called.


Case Where Result Is Different

Here is an example of a case where dot and function notation can give different results.
Suppose that you have the following classes:


  • classA defines a method called methodA that requires an object of classB as one of
    its arguments

  • classB defines classA as inferior to classB


9 Methods — Defining Class Operations

Free download pdf