MATLAB Object-Oriented Programming

(Joyce) #1

obj.Tuesday('11')


Controlling Access to Methods


There can be situations where you want to create methods for internal computation
within the class, but do not want to publish these methods as part of the public interface
to the class. In these cases, you can use the Access attribute to set the access to one of
the following options:



  • public — Any code having access to an object of the class can access this method
    (the default).

  • private — Restricts method access to the defining class, excluding subclasses.
    Subclasses do not inherit private methods.

  • protected — Restricts method access to the defining class and subclasses derived
    from the defining class. Subclasses inherit this method.

  • Access list — Restricts method access to classes in access list. For more information,
    see “Class Members Access” on page 12-28


Local and nested functions inside the method files have the same access as the method.
Local functions inside a class-definition file have private access to the class defined in the
same file.


Invoking Superclass Methods in Subclass Methods


A subclass can override the implementation of a method defined in a superclass. If the
subclass method needs to execute additional code instead of completely replacing the
superclass method. MATLAB classes can use a special syntax for invocation of superclass
methods from a subclass implementation for the same-named method.


The syntax to call a superclass method in a subclass class uses the @ symbol:


MethodName@SuperclassName


For example, the following disp method is defined for a Stock class that is derived from
an Asset class. The method first calls the Asset class disp method, passing the Stock
object so that the Asset components of the Stock object can be displayed. After the
Asset disp method returns, the Stock disp method displays the two Stock properties:


classdef Stock < Asset
methods
function disp(s)


Method Invocation
Free download pdf