MATLAB Object-Oriented Programming

(Joyce) #1

b.m1;


Method m1 defined by AcListNonSub


Subclasses Without Access


Including the defining class in the access list for a method grants access to all subclasses
derived from that class. When you derive from a class that has a method with an access
list and that list does not include the defining class:



  • Subclass methods cannot call the superclass method.

  • Subclass methods can call the superclass method indirectly using an instance of a
    class that is in the access list.

  • Subclasses cannot override the superclass method.

  • Methods of classes that are in the superclass method access list, but that are not
    subclasses, can call the superclass method.


For example, AcListSub is a subclass of AcListSuper. The AcListSuper class defines
an access list for method m1. However, this list does not include AcListSuper, so
subclasses of AcListSuper do not have access to method m1:


classdef AcListSub < AcListSuper
methods
function obj = sub1(obj,AcListSuper_Obj)
% Access m1 via superclass object (NOT ALLOWED)
AcListSuper_Obj.m1;
end
function obj = sub2(obj,AcListNonSub_Obj,AcListSuper_obj)
% Access m1 via object that is in access list (is allowed)
AcListNonSub_Obj.nonSub1(AcListSuper_Obj);
end
end
end


No Direct Call to Superclass Method


Attempting to call the superclass m1 method from the sub1 method results in an error
because subclasses are not in the access list for m1:


a = AcListSuper;
c = AcListSub;
c.sub1(a);


Cannot access method 'm1' in class 'AcListSuper'.


Class Members Access
Free download pdf