Inheritance in Matlab Object Oriented Programming - Matlab OOP 10 | applied electronics engineering


Inheritance in Matlab Object Oriented Programming - Matlab OOP 10

By Applied Electronics - Friday, February 10, 2017 No Comments
In this matlab object oriented programming tutorial we explain what is inheritance and how it works in Matlab software. This is the 10th tutorial of Object Oriented Programing with Matlab.

Inheritance in object oriented programming is a phenomenon in which a class inherits(or acquires) properties or methods or both from another class. The class which inherits is called subclass or the child class and the class from which a class inherits is called superclass or the parent class.

If for example rect is a superclass and box is a subclass, then box will inherit properties or methods or both from rect class. The subclass can also have its own properties and methods besides the inherited properties and methods.

To tell Matlab software that a class is a subclass of another class the operator '<' is used. And for inheritance of properties the '@' symbol is used.

Example of Inheritance in Matlab Object Oriented Programming

Consider that rect is a super class with properties length l and width w. The definition of class rect is as follows.

classdef rect

        l = 0;
        w = 0;
        function rectObj = rect(v1, v2)
            if nargin > 0
                rectObj.l = v1;
                rectObj.w = v2;

Lets now create a sub-class box which inherits length l and width w from rect super class. Also let the box has its own property of height h. Then the class would be written as follows:

classdef box < rect
        h = 0;
        function boxObj = box(v1, v2, v3)
            if nargin < 3
                v1 = 0;
                v2 = 0;
                v3 = 0;
            boxObj@rect(v1, v2);  %inheritance of v1 and v2
            boxObj.h = v3;
        function calVol = vol(boxObj)
            calVol = boxObj.l*boxObj.w*boxObj.h;

As you can see we have used the < operator to signify that this box class is a subclass of rect. Then we have declared the box properties. In the method we have created a constuctor for the box class. The constructor object height is the same as the initialized height h. However, since we want to inherit the properties length l and width w we have used the box object followed by @ symbol followed by the rect class constructor and the arguments, that is boxObj@rect(v1,v2).At the end you can see a method called vol that calculates the volume of the box.

Creating Object

Now we will show how to create object of class box and show that it has inherited the length(l ) and width(w) from the rect class. See the following code:

>> box1 = box

box1 = 

  box with properties:

    h: 0
    l: 0
    w: 0

This shows that the box1 object of subclass box has inherited the length l and width w from the superclass rect.

We can specify the h, l and w now and calculate the volume of the box using the vol method.

>> box1.l = 4;
>> box1.w = 2;
>> box1.h = 2;
>> box1.vol

ans =


So in this way the inheritance in Matlab Object Oriented class and object works.
The things to remember in using inheritance in matlab is:
  1. To use the < operator during the declaring the class name. This operator tells whether the class being created is a subclass and if so which is the superclass.
  2. To use the @ symbol while specifying the properties and method to be inherited

No Comment to " Inheritance in Matlab Object Oriented Programming - Matlab OOP 10 "