Hierarchy in MATLAB

Introduction to Hierarchy

This blog is part 2 on the object oriented programming in Matlab. Consider reading out previous blog first.

In classifying objects, hierarchy in the objects is a common occurrence, where subclasses and super-classes can be defined. In the example of a rectangle class, a superclass namely Shape can be defined, where the superclass provides a more broadly defined class for a shape, and rectangle a more narrowly defined class for a specific type of shape. We can extend our example, by including the creation of Triangle and Circle classes, which are also subclasses of the superclass Shape.

Firstly, inheritance of a common property is discussed, and secondly inheritance of a common method function using Abstract classes is discussed.

The shapes rectangle, triangle, and circle share a common property, namely for each of these shapes a Colour property is defined. In creating a superclass shape, the sub-classes can inherit this Colour property from the superclass, and the property Colour will not need to be defined under each shape sub-class.

Class definition code in MATLAB: Shape

classdef Shape < handle
    % Shape Data set class
    properties
        Colour
    end
    methods
        function thisShape=Shape(colour)
            if nargin>0
                thisShape.Colour=colour;
            end
        end
    end
end

Creating a superclass is useful as it reduces duplication in the code- the subclasses inherit properties and method functions from the superclass and therefore the code need not be defined again in each subclass. Reducing duplicates in the code enables easier and more efficient error checking as the code only needs to be checked and modified in one class (the superclass), in contrast to modifying the code in all the subclasses.

MATLAB Syntax and application for creating super and sub classes

The “<” symbol is used in MATLAB to define inheritance. For example, in the class definition of Rectangle, the “<” symbol will be used to indicate that the Rectangle class inherits from the superclass Shape.

classdef Rectangle < Shape

The property Colour does not need to be specified under properties in the Rectangle class code as it is a property which is inherited from the superclass Shape. In the subclass constructor function a call to the superclass constructor function is made to pull the value of the inherited property. The “@” symbol is used to call the superclass constructor as seen in the code below:

thisRectangle@Shape(colour)

(Similarly, the class definitions for circle and triangle will be adjusted accordingly.)

From this simple example, one can see that creating a superclass is extremely useful, and the utilisation of such relationship structures in more complex models will help the refinement and design of models where code reuse is necessary.

Class definition code in MATLAB: Rectangle

Abstract Methods

It is noted, that for each shape the area is calculated, but the formula used to calculate the area for each shape is different, for example for a rectangle the area is calculated as width multiplied by height, but for a circle the area is calculated as pi multiplied by radius squared etc.  It follows, that the getArea function under methods is common for all the shapes and can be listed in the class definition for Shape as an abstract method.

An optional attribute Abstract is defined for the method function getArea. In assigning the optional attribute Abstract, all the sub-classes must contain a method function called getArea. It is one way to control, that by definition, for all shapes a function to calculate the area must be defined. If a superclass method function defines a method function as Abstract, and if you do not specify that function within the subclass, an error message will be sent by MATLAB saying the following:

Class definition code in MATLAB: Shape

Property Values: Initialisation and Validation

The assignment of default property values and validation of property values can be performed in the property code or in the constructor function. (Property validation methods can also be performed using property get and set methods, but this is not covered in this demo.)

Example: Property Values

We are interested in assigning default values, and performing validation method functions for the properties of the class Rectangle. In the table below, a list of properties and the various values and validation functions are defined.

Property Class Type Default Values Validation function
Colour Char 'green' MustBeMember: the Colour must be specified as either 'red', 'green' or 'blue'.
Width Double 10 MustBeNumeric: the Width must be a numeric value
Height Double 10 MustBeNumeric: the Height must be a numeric value
  1. The property definition

In the property definition:

  • the size of the property,
  • the validation functions, and
  • the default values of the property values can be defined.

Properties definition code in MATLAB:

Using the property definition to assign and validate property values for the class Rectangle is defined below.

Properties definition code in MATLAB: Rectangle

    properties
        
        Width double {mustBeNumeric}=10
        
        Height double {mustBeNumeric}=10
        
        Colour char {mustBeMember(Colour,{'red','blue','green'})} ='green'
        
    end
  1. The constructor method function

Using the constructor function to initialise and validate input argument values for the Rectangle class.

In addition, to setting default values and validation of property values, the constructor function can also perform checks and send error messages if the incorrect number of arguments are specified as inputs in the constructor function.

Constructor function code including initialisation and validation of property values in MATLAB: Rectangle

  • It is possible to use a combination of the property definition and the constructor function to specify property values.

Hierarchy

By calling the superclass constructor function in subclasses, the property values only need to be initialised and validated in one section of the code (the superclass code block), this can lead to fewer errors in assigning the correct property values.

References

(1) MATLAB: Object Oriented Programming (R2016b). 9th ed. Natwick, MA: MathWorks, 2016.

One thought on “Hierarchy in MATLAB

Leave a Reply

Your email address will not be published. Required fields are marked *