Skip to Main Content Skip to Search
Product Documentation

Modifying Superclass Methods and Properties

Modifying Superclass Methods

An important concept in class design is that a subclass object is also an object of its superclass. Therefore, you can pass a subclass object to a superclass method and have the method execute properly. At the same time, you can apply special processing to the unique aspects of the subclass. Some useful techniques include:

Extending Superclass Methods

Subclass methods can call superclass methods of the same name. This fact enables you to extend a superclass method in a subclass without completely redefining the superclass method. For example, suppose that both superclass and subclass defines a method called foo. The method names are the same so the subclass method can call the superclass method. However, the subclass method can also perform other steps before and after the call to the superclass method. It can 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 executes in a series of steps using a protected method for each step (Access attribute set to protected). Subclasses can then 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)
         step2(obj)
         step3(obj)
      end
   end
   methods (Access = protected)
      function step1(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.

classdef sub < super
   ...
   methods (Access = protected)
      function step1(obj)
         subclass version
      end
      ...
   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 two separate conditions under which you can 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.

Private Local Property Takes Precedence in Method

When a subclass property has the same name as a superclass private property, and a method of the superclass references the property name, MATLAB always accesses the property defined by the calling method's class. For example, given the following classes, Sub and Super:

classdef Super
   properties (Access = private)
      Prop = 2;
   end
   methods
      function p = superMethod(obj)
         p = obj.Prop; 
      end
   end
end
classdef Sub < Super
   properties 
      Prop = 1;
   end
end

If you create an instance of the subclass and use it to call the superclass method, MATLAB access the private property of the method's class:

>> subObj = Sub
subObj = 

  Sub

  Properties:
    Prop: 1

  Methods, Superclasses
>> subObj.superMethod
ans =

     2
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

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