Open In App

Polymorphism in MATLAB

Last Updated : 13 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

As with all other programming languages, MATLAB is also a programming language specially designed for Engineers. It was designed by Mathworks. Like all other programming languages, it also supports the concept of Object-oriented programming language (OOPs).

The main concept is called Polymorphism. it is defined as a process in which a function which is already existed with the same name is called Polymorphism.

It is also defined as one form in many which mean different functions with the same name which is also called Overriding.

In MATLAB Polynomial functions are generally used for math problems it perform different operations with the same name based on the passed arguments and user-defined logic.

Now we can understand it clearly with the help of an example :

Example 1:

Matlab




% Code for polymorphism in MATLAB
classdef ParentClass
     
                 methods
      
                 function testing(self)
                   disp('This is Parent Class');
          end
          
      end
      
end


In ParentClass code we first defined a class whose name is ParentClass by using convention then we create a function name testing  with its parameters then we will write the paragraph or statement which we want to display then we first end the function then we end the method created in class and at last end the class.

Now save the above class with ParentClass.m and see its child class or work on ChildClass.

ChildClass:

Matlab




% Code for polymorphism in MATLAB
classdef ChildClass < ParentClass
      
     methods
      
       % Overriding the ParentClass method
             function testing(self)
               disp('This is ChildClass');
           end
        end
      
end


Output:

 

Explanation:

Now in ChildClass first we have to drive the properties of parent classthe  by creating  a class with name “Childclass < ParentClass”. then by using the concept of polymorphism we define a function with the same name and its parameters then write the statement which we want to display then we first end the function the method created and then the class.

Now In the output, we have to create the objects of both classes to execute statements. by “abc” we create an object for ParentClass and by “XYZ” we create an object for the child class So the conclusion is that it works similarly in MATLAB as it works in other Programming languages like C++, Java, Python, etc.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads