Open In App

Overloading Functions in Class Definitions in MATLAB

Last Updated : 01 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The overloading Function is like in all other programming languages the definition of Overloading functions. It is basically defined as more than one function with the same name but different parameters and data types. In other words, function overloading is the feature of Object Oriented Programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. It can be considered an example of Polymorphism in MATLAB.

It is the concept of using more than one function with the same name to perform different tasks in the same program suppose for example if we have to calculate the sum of two numbers. Now the case is that one time we have to add integer values and in another case, we have to add floating values So to encounter this problem we can use the Overloading function with the same name but different parameters.

  1. sum_of_numbers(int x, int y) for the sum of integer values.
  2. sum_of_numbers(float a , float b , float c) for the sum of decimal values. 

We can easily understand it with the help of the Example :

Example 1: 

Matlab




% Code for polymorphism in MATLAB
classdef Vehicle
     
                methods
     
                function testing(self)
                disp('This is a car ');
                 
                function testing(self)
                disp('This is a Motorcycle');
        end
    end
end


So here in this program we first create a class name Vehicle then we define two functions with the same name but different functions. Now in output, we will see the working of both functions.

Output :

 

Explanation:

So Here in this program, there are two functions with the same name In the output we first call the first function after calling the first function we get the statements of the first function like as :

xyz = Vehicle;

testing(xyz);

–) This is a car 

  Now we call the function again with the same process then we get the result of the second function the statement of the second function will be displayed on the output shell.

xyz = Vehicle;

testing(xyz);

–) This is a Motorcycle 

So the conclusion is that it works similarly like to it works in other programming languages.

There are some points or a few steps keep in mind while writing an Overloaded MATLAB program these points are as follows:

  1. First of all, we have to define a method that has the same name as the function in the MATLAB program.
  2. Now we have to check or verify that the method we have declared has accepted the object of the class.
  3. Now we have to check that the method which contains the object of the class uses the same version of MATLAB.
  4. Now we have to complete all the other required steps to generate the output of the program or code. 

So it works similarly to all other programming languages there is no specification to write an overloaded program it is just simple and basic in all other programming languages same is in MATLAB. Because it is also a High-level programming language.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads