Object-Oriented Programming (OOPs) in MATLAB is similar to many conventional programming languages like Java, Python etc except the syntax. The important feature of OOPs is, it enables you to combine data and it’s associated actions (methods/functions) into objects. Compared to other languages, such as C++, Java etc, Object-Oriented Programming will be much faster in MATLAB which enables you to solve complex computing applications. There are certain features in OOPs which can be used for solving complex problems, providing security to the data etc.
OOPs features supported by MATLAB are:
Now let’s dive into some examples to understand the above concepts better.
A Class is a template/blueprint defined by a user using which objects are created. It comprises of a set of attributes and methods which are used for object entities. A Constructor is method/function, defined with the same name of the class, and it initializes the classes with certain values passed through calling function (i.e Constructor). Defining a constructor is optional as in many other languages. MATLAB syntax is quite peculiar compared to other conventional programming languages.
classdef (attributes) className
. . . . .
classdef -> keyword used to define classes
attributes -> Arguments/Values used to modify the behaviour of class
Save the above code as adder.m file and access it with commands from command prompt/another matlab file. Let’s access the above code by giving some commands from another matlab file and observe the output to understand it better.
Save the above code as testing.m. After executing the above file, you will get the following output.
An Object is a logical entity that interacts with the user-defined class by invoking methods/functions. We can create as many objects as you want. Every object will have two characteristics i.e., state and behavior. These characteristics vary with every object as per the situation. For example, dog is an object/identity. It’s loyalty, sleep, walk etc are behaviors and size, color, breed etc comes under the state.
The title itself suggests that inheritance is nothing but inheriting/acquiring all the properties, such as attributes, methods etc, of parent/base class. The class that is derived from a base class is called as Sub Class/Child Class and it inherits all the properties of its parents class. Superclass is the class that is being inherited by the subclass. The importance of the inheritance is the code reusability such as using the parent class’s attributes, methods etc. Let’s dive into an example to understand the inheritance concept better.
classdef subClassName < SuperClass
. . . . . . .
Save the above code as Animal.m and this is our superclass. Let us see subclass now.
Save the above code as Lion.m and this is our subclass. Now let’s access the parent class using subclass object and observe the output.
The mechanism of defining a function with the name of the already existed function is known as polymorphism. Simply we call it as overriding. MATLAB doesn’t support overloading a function. The polymorphic functions perform different operations with the same name based on the passed arguments and user-defined logic.
Save the above code as superClass.m and let’s also see the derived class code.
Save the above code as derivedClass.m and let’s observe the output.
An Abstract class depicts the functionality performed by a group of classes. It hides important or unnecessary data and shows only the essential components. For instance, A mobile can be viewed but not its inner components. All the methods, attributes that are being used by the subclasses are declared in the abstract class. A concrete subclass is derived from abstract class to use its properties. An abstract can’t be instantiated. It can only be inherited. All the methods declared in the abstract class must be redefined/overridden in the subclass.
classdef (Abstract) className:
. . . . .
Save the above code as superClass.m and let’s see the code for the base class.
Save the above code as derivedClass.m and execute the code and observe the output by giving a couple of commands.
Prerequisite: MATLAB documentation of Value and Handle Classes
Encapsulation is a mechanism of enclosing the data and the code together in a single class/unit. It provides security to the data by prohibiting the limit to other classes. One can access or modify the data of a class only by using getters and setters methods. We need to inherit handle class to implement encapsulation in MATLAB as it doesn’t return duplicate and modified objects and also handles the errors.
Save the above code as superClass.m and let’s observe the output by giving a couple of commands.
- In MATLAB, Class variables are called as Properties.
- In MATLAB, Classes and methods are defined in separate files.
- We have to save the class files and method files with their respective names.