MATLAB Object-Oriented Programming

(Joyce) #1

  • Define their own method with the same name (if not a subclass).

  • Override the method in a subclass only if the superclass defining the method includes
    itself or the subclass in the access list.


These sample classes show the behavior of methods called from methods of other classes
that are in the access list. The class AcListSuper gives the AcListNonSub class access
to its m1 method:

classdef AcListSuper
methods (Access = {?AcListNonSub})
function obj = m1(obj)
disp ('Method m1 called')
end
end
end

Because AcListNonSub is in the access list of m1, its methods can call m1 using an
instance of AcListSuper:

classdef AcListNonSub
methods
function obj = nonSub1(obj,AcListSuper_Obj)
% Call m1 on AcListSuper class
AcListSuper_Obj.m1;
end
function obj = m1(obj)
% Define a method named m1
disp(['Method m1 defined by ',class(obj)])
end
end
end

Create objects of both classes:

a = AcListSuper;
b = AcListNonSub;

Call the AcListSuper m1 method using an AcListNonSub method:

b.nonSub1(a);

Method m1 called

Call the AcListNonSub m1 method:

12 How to Build on Other Classes

Free download pdf