Object Oriented Programming (OOPs) in MATLAB
Last Updated :
17 Aug, 2021
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 that can be used for solving complex problems, providing security to the data, etc.
OOPs features supported by MATLAB are:
- Object
- Class
- Polymorphism
- Inheritance
- Abstraction
- Encapsulation
Now let's dive into some examples to understand the above concepts better.
Class
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 a method/function, defined with the same name of the class, and it initializes the classes with certain values passed through the 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.
Syntax:
classdef (attributes) className
. . . . .
end
Parameters:
classdef -> keyword used to define classes
attributes -> Arguments/Values used to modify the behaviour of class
Example:
Matlab
% MATLAB program to illustrate Classes
classdef adder
% Attributes of a class are defined
% in properties block
properties
value
end
% Function/Methods of class are defined
% in methods block
methods
function res = addVal(obj)
res = obj.value+obj.value;
end
end
end
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.
Matlab
% MATLAB program to access the
% user defined classes
% Initiating the Class
temp=testing();
% Assign values to the class attributes
temp.value=5;
% Calling methods inside the class
addVal(temp)
Save the above code as testing.m. After executing the above file, you will get the following output.
OutputObject
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.
Inheritance
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.
Syntax:
classdef subClassName < SuperClass
. . . . . . .
end
Super Class:
Matlab
% MATLAB program to illustrate Super class
% and inheritance.
classdef Animal
methods
function eat(self)
disp('I am Eating');
end
function sleep(self)
disp('I am Sleeping');
end
end
end
Save the above code as Animal.m and this is our superclass. Let us see subclass now.
Matlab
% MATLAB program to illustrate
% sub class and inheritance
classdef Lion < Animal
methods
function roar(self)
disp('Roaring');
end
end
end
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.
Output:
OutputPolymorphism
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.
SuperClass:
Matlab
% An example of polymorphism in MATLAB
classdef superClass
methods
function testing(self)
disp('This is superClass');
end
end
end
Save the above code as superClass.m and let's also see the derived class code.
Derived Class:
Matlab
% An example of polymorphism in MATLAB
classdef derivedClass < superClass
methods
% Overriding the superClass method
function testing(self)
disp('This is derivedClass');
end
end
end
Save the above code as derivedClass.m and let's observe the output.
Output:
OutputAbstraction
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.
Syntax:
classdef (Abstract) className:
. . . . .
end
Example:
Matlab
% An example of abstract class in MATLAB
classdef (Abstract) superClass
% Declaring abstract properties
properties (Abstract)
value1
end
% Declaring abstract methods
methods (Abstract)
checking(obj)
end
end
Save the above code as superClass.m and let's see the code for the base class.
Matlab
% An example of abstract base class in MATLAB
classdef derivedClass < superClass
% Redefining abstract properties
properties
value1 = 0;
end
% Redefining abstract methods
methods
function checking(self)
disp('This is abstract method of superClass and redefined in the derivedClass');
fprintf('The initial of the property "value1" is %d',self.value1);
end
end
end
Save the above code as derivedClass.m and execute the code and observe the output by giving a couple of commands.
Output:
OutputEncapsulation
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.
MATLAB -
Save the above code as superClass.m and let's observe the output by giving a couple of commands.
Output:
Output
Note:
- 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.
Similar Reads
Best Practices of Object Oriented Programming (OOP)
As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance , abstraction , polymorphism, and encapsulation in programming. The main aim of OOP is to bind together th
5 min read
Design Goals and Principles of Object Oriented Programming
The fundamental goal of dealing with the complexity of building modern software naturally gives rise to several sub-goals. These sub-goals are directed at the production of quality software, including good implementations of data structures and algorithms. The article focuses on discussing design go
13 min read
Operator Overloading in MATLAB
MATLAB allows you to specify more than one definition for an operator in the same scope which is called Operator Overloading. We can redefine or overload most of the built-in operators available in MATLAB. It is basically a type of polymorphism in which an operator is overloaded to give user-defined
3 min read
Differences between Procedural and Object Oriented Programming
This article focuses on discussing the differences between procedural and object-oriented programming. Procedural Programming Procedural Programming can be defined as a programming model which is derived from structured programming, based upon the concept of calling procedure. Procedures, also known
2 min read
Restore Warnings in MATLAB
MATLAB shows warning whenever an error or exception occurs. Generally, these warnings are insightful however, sometimes they are not needed or are needed in a modified state. For such cases, MATLAB provides the option to modify or define one's own warnings for an error/exception. But, there could be
3 min read
Simple Input/Output Program in MATLAB
Let us see how to input and output data in MATLAB. input() Syntax : input(PROMPT, "s") Parameters : PROMPT : text prompted "s" : optional, to input a string Returns : the data entered The input() function is used to input data in MATLAB. Example : MATLAB % entering an integer input("Enter an in
1 min read
Random Numbers in MATLAB
Random numbers, as the name suggests, are numbers chosen randomly from a set of numbers. In practical application, classical computers cannot create truly random numbers as they are developed on binary logic thus, they require some sort of algorithm to generate random numbers, this type of random nu
2 min read
Object-Oriented Analysis and Design(OOAD)
Object-Oriented Analysis and Design (OOAD) is a way to design software by thinking of everything as objects similar to real-life things. In OOAD, we first understand what the system needs to do, then identify key objects, and finally decide how these objects will work together. This approach helps m
6 min read
Object Oriented Testing in Software Testing
In traditional software testing, the focus was on checking how functions or procedures operated on data. But with the rise of object-oriented programming (OOP), the approach to testing has shifted. Now, testing is more about checking how objects and classes behave and how they interact with each oth
6 min read
Matlab Floating Point Precision
Numeric class in MATLAB includes signed and unsigned integers, single-precision floating-point numbers, and double-precision floating-point numbers. Generally, MATLAB stores all numeric values as double-precision floating-point.Floating Point Numbers in MATLAB are stored in two forms: Single Precisi
2 min read