MATLAB Object-Oriented Programming

(Joyce) #1

Two points about super and subclass behavior to keep in mind are:



  • Methods defined in the superclass can operate on subclass objects.

  • Methods defined in the subclass cannot operate on superclass objects.


Therefore, you can treat an Engineer object like any other Employees object, but an
Employee object cannot pass for an Engineer object.


Limitations to Object Substitution


MATLAB determines the class of an object based on its most specific class. Therefore, an
Engineer object is of class Engineer, while it is also an Employees object, as using
the isa function reveals.


Generally, MATLAB does not allow you to create arrays containing a mix of superclass and
subclass objects because an array can be of only one class. If you attempt to concatenate
objects of different classes, MATLAB looks for a converter method defined by the less
dominant class


See “Concatenating Objects of Different Classes” on page 10-18 for more information.


See matlab.mixin.Heterogeneous for information on defining heterogeneous class
hierarchies.


See “Object Converters” on page 17-12 for information on defining converter methods.


Implementation and Interface Inheritance


MATLAB classes support both the inheritance of implemented methods from a superclass
and the inheritance of interfaces defined by abstract methods in the superclass.


Implementation inheritance enables code reuse by subclasses. For example, an employee
class can have a submitStatus method that all employee subclasses can use.
Subclasses can extend an inherited method to provide specialized functionality, while
reusing the common aspects. See “Modify Inherited Methods” on page 12-16 for more
information on this process.


Interface inheritance is useful in these cases:



  • You want a group of classes to provide a common interface.

  • Subclasses create specialized implementations of methods and properties.


Hierarchies of Classes — Concepts
Free download pdf