MATLAB Object-Oriented Programming

(Joyce) #1
Error in AcListSub/sub1 (line 4)
AcListSuper_Obj.m1;

Indirect Call to Superclass Method

You can call a superclass method from a subclass that does not have access to that
method using an object of a class that is in the superclass method access list.

The AcListSub sub2 method calls a method of a class (AcListNonSub) that is on the
access list for m1. This method, nonSub1, does have access to the superclass m1 method:

a = AcListSuper;
b = AcListNonSub;
c = AcListSub;
c.sub2(b,a);

Method m1 called

No Redefining Superclass Method

When subclasses are not included in the access list for a method, those subclasses cannot
define a method with the same name. This behavior is not the same as cases in which the
method Access is explicitly declared as private.

For example, adding the following method to the AcListSub class definition produces an
error when you attempt to instantiate the class.

methods (Access = {?AcListNonSub})
function obj = m1(obj)
disp('AcListSub m1 method')
end
end
c = AcListSub;
Class 'AcListSub' is not allowed to override the method 'm1' because neither it nor its
superclasses have been granted access to the method by class 'AcListSuper'.

Call Superclass from Listed Class Via Subclass

The AcListNonSub class is in the m1 method access list. This class can define a method
that calls the m1 method using an object of the AcListSub class. While AcListSub is not
in the access list for method m1, it is a subclass of AcListSuper.

For example, add the following method to the AcListNonSub class:

12 How to Build on Other Classes

Free download pdf