MATLAB Object-Oriented Programming

(Joyce) #1

Modify Inherited Methods


In this section...
“When to Modify Superclass Methods” on page 12-16
“Extend Superclass Methods” on page 12-16
“Redefine Superclass Methods in Subclass” on page 12-17
“Override Superclass Methods” on page 12-18

When to Modify Superclass Methods


Class design enable you to pass a subclass object to a superclass method and have the
method execute properly. At the same time, subclass methods can apply special
processing to the unique aspects of the subclass. Some useful techniques include:


  • Extend — Calling a superclass method from within a subclass method

  • Redefine — Reimplementing in the subclass, the protected methods that are called
    from within a public superclass method

  • Override — Defining the same named methods in both super and subclass, but using
    different implementations


Extend Superclass Methods


Subclass methods can call superclass methods of the same name. This technique enables
you to extend a superclass method in a subclass without completely redefining the
superclass method.

For example, suppose that both superclass and subclass define 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 that calls the superclass foo method

classdef Sub < Super
methods
function foo(obj)
% preprocessing steps

12 How to Build on Other Classes

Free download pdf