Modifying Superclass Methods and Properties

Modifying Superclass Methods

An important concept to keep in mind when designing classes is that a subclass object is also an object of its superclass. Therefore, you should be able to pass a subclass object to a superclass method and have the method execute properly. At the same time, you might need to apply special processing to the unique aspects of the subclass. Some useful techniques to do this include:

Extending Superclass Methods

Subclass methods can call superclass methods of the same name. This enables you to extend a superclass method in a subclass without completely redefining the superclass method. For example, suppose both superclass and subclass define a method called foo. The method names are the same so the subclass method can call the superclass method, but the subclass method can also perform other steps before and after calling the call to the superclass method to operate on the specialized parts to the subclass that are not part of the superclass.

For example this subclass defines a foo method, which calls the superclass foo method

classdef sub < super
   methods
      function foo(obj)
         preprocessing steps 
         foo@super(obj); % Call superclass foo method
         postprocessing steps
      end 
   end
end

See Invoking Superclass Methods in Subclass Methods for more on this syntax.

Completing Superclass Methods

A superclass method can define a process that is executed in a series of steps using a protected method for each step (Access attribute set protected). Subclasses can then create their own versions of the protected methods that implement the individual steps in the process.

The implementation of this technique would works as shown here:

classdef super
   methods 
      function foo(obj)
         step1(obj)
         step2(obj)
         step3(obj)
      end
   end
   methods (Access = protected)
      function step1(obj)
         superclass version
      end
      etc.
   end
end

The subclass would not reimplement the foo method, it would reimplement 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® dispatching rules ensure the subclass step methods are called.

classdef sub < super
   ...
   methods (Access = protected)
      function step1(obj)
         subclass version
      end
      etc.
   end
end

Redefining Superclass Methods

You can completely redefine a superclass method. In this case, both the superclass and the subclass would define the same named method.

Modifying Superclass Properties

There are only two conditions that allow you to redefine superclass properties:

In the first case, the superclass is just requesting that you define a concrete version of this property to ensure a consistent interface. In the second case, only the superclass can access the private property, so the subclass is free to reimplement it in any way.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS