Main Content

Modify Inherited Methods

When to Modify Superclass Methods

Class designs enable you to pass subclass objects to superclass methods. The superclass method executes properly because the subclass object is a superclass object. However, subclasses can implement their own versions of superclass methods, which MATLAB® calls when passed subclass objects.

Subclasses override inherited methods (that is, implement a method of the same name) when there is a need to provide specialized behavior in the subclass. Here are some patterns that override superclass methods.

  • Extend the superclass method by calling it from within the subclass method. The subclass method can perform subclass-specific processing in addition to calling the superclass method.

  • In a superclass method, implement a series of steps in a procedure using protected methods. Then reimplement these steps in a subclass method by redefining the protected methods that are called from within a public superclass method.

  • Redefine the same-named method in the subclass, but use different implementations to perform the same operation differently on subclass objects.

  • Implement abstract superclass methods in the subclass. Abstract superclasses can define methods with no implementation and rely on subclasses to provide the implementation. For more information, see Define an Interface Superclass.

Subclass methods that override superclass methods must define the same value for the Access attribute as is defined by the superclass method.

Extend Superclass Methods

Calling the same-named superclass method from a subclass method enables you to extend the superclass method for subclass objects without affecting the superclass method.

For example, suppose that both superclass and subclass define a method called foo. The subclass method calls the superclass method and performs other steps in addition to the call to the superclass method. The subclass method can operate on the specialized parts to the subclass that are not part of the superclass.

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

classdef Sub < Super
   methods
      function foo(obj)
         % preprocessing steps
          ...
         foo@Super(obj);
         % postprocessing steps
          ...
      end
   end
end

Reimplement Superclass Process in Subclass

A superclass method can define a process that executes in a series of steps using a method for each step (typically Access attribute is set to protected for the step methods). This pattern (referred to as a template method) enables subclasses to create their own versions of the protected methods that implement the individual steps in the process. The process is specialized for the subclass.

Implement this technique as shown here:

classdef Super
   methods (Sealed)
      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 override the foo method. Instead it overrides only the protected methods that perform the series of steps (step1(obj), step2(obj), step3(obj)). This technique enables the subclass to specialize the actions taken by each step, but 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. For more information on method dispatching, see Method Invocation.

classdef Sub < Super
   ...
   methods (Access = protected)
      function step1(obj)
         % Subclass version
      end
      function step2(obj)
         % Subclass version
      end
      function step3(obj)
         % Subclass version
      end
      ...
   end
end

Redefine Superclass Methods

You can completely redefine a superclass method in a subclass. In this case, both the superclass and the subclass would define a method with the same name. However, the implementation would be different and the subclass method would not call the superclass method. Creating independent versions of the same-named method can be necessary when the same operation requires different implementation on the superclass and subclass.

classdef Super
   methods
      function foo(obj)
         % Superclass implementation
      end
   end
end
classdef Sub < Super
   methods
      function foo(obj)
         % Subclass implementation
      end
   end
end

Implement Abstract Method in Subclass

Abstract methods have no implementation. Subclasses that inherit abstract methods must provide a subclass-specific implementation for the subclass to be a concrete class. For more information, see Abstract Classes and Class Members.

classdef Super
   methods (Abstract)
      foo(obj)
         % Abstract method without implementation
      end
   end
end
classdef Sub < Super
   methods
      function foo(obj)
         % Subclass implementation of concrete method
      end
   end
end

Related Topics