MATLAB Object-Oriented Programming

(Joyce) #1

...
foo@Super(obj);
% postprocessing steps
...
end
end
end


Redefine Superclass Methods in Subclass


A superclass method can define a process that executes in a series of steps using a
protected method for each step (Access attribute set to protected). Then, subclasses
can create their own versions of the protected methods that implement the individual
steps in the process.


Implement this technique as shown here:


classdef Super
methods
function foo(obj)
step1(obj) % Call step1
step2(obj) % Call step2
step3(obj) % Call step3
end
end
methods (Access = protected)
function step1(obj)
% Superclass version
end
function step2(obj)
% Superclass version
end
function step3(obj)
% Superclass version
end
end
end


The subclass does not reimplement the foo method, it reimplements only the methods
that carry out the series of steps (step1(obj), step2(obj), step3(obj)). That is, the
subclass can specialize the actions taken by each step, but does not control the order of
the steps in the process. When you pass a subclass object to the superclass foo method,
MATLAB calls the subclass step methods because of the dispatching rules.


Modify Inherited Methods
Free download pdf