MATLAB Object-Oriented Programming

(Joyce) #1
When designing classes, your abstraction contains only those elements that are necessary.
For example, the employee hair color and shoe size certainly characterize the employee,
but are probably not relevant to the design of this employee class. Their sales region is
relevant only to some employee so this characteristic belongs in a subclass.

Design of Class Hierarchies


As you design a system of classes, put common data and functionality in a superclass,
which you then use to derive subclasses. The subclasses inherit the data and functionality
of the superclass and define only aspects that are unique to their particular purposes.
This approach provides advantages:


  • Avoid duplicating code that is common to all classes.

  • Add or change subclasses at any time without modifying the superclass or affecting
    other subclasses.

  • If the superclass changes (for example, all employees are assigned a number), then
    the subclass automatically get these changes.


Super and Subclass Behavior


Subclass objects behave like objects of the superclass because they are specializations of
the superclass. This fact facilitates the development of related classes that behave
similarly, but are implemented differently.

A Subclass Object β€œIs A” Superclass Object

You can usually describe the relationship between an object of a subclass and an object of
its superclass with a statement like:

The subclass is a superclass. For example: An Engineer is an Employee.

This relationship implies that objects belonging to a subclass have the same properties,
methods, and events as the superclass. Subclass objects also have any new features
defined by the subclass. Test this relationship with the isa function.

Treat Subclass Objects like Superclass Objects

You can pass a subclass object to a superclass method, but you can access only those
properties that the superclass defines. This behavior enables you to modify the subclasses
without affecting the superclass.

12 How to Build on Other Classes

Free download pdf