Overloading Functions in Class Definitions in MATLAB


In MATLAB, when we define multiple methods or functions with the same name but a list of different input arguments in a class, it is known as function overloading. In MATLAB programming, the function overloading is a part of polymorphism. Where, the concept of polymorphism says that a single function can behave differently depending on the nature or type of input arguments provided.

This article is primarily meant for explaining the concept of function overloading in class definitions in MATLAB programming. But before that let us learn a bit about overloading functions in class definitions in MATLAB.

What is Overloading a Function in MATLAB?

MATLAB is a high-level programming language that provides the ability to define multiple functions or methods with the same name in a class. Where, each function can take a different set of input arguments. This concept is known as function overloading.

Therefore, a particular set of input arguments determines which function is to involve. In MATLAB programming, the concept of overloading a function is used to implement a function that can behave differently depending on the situation. Hence, using this concept, we can create classes with more flexible interfaces in MATLAB.

Here is an example showing the implementation of function overloading in class definition in MATLAB −

classdef SampleClass
   methods
      function fun(obj)
         disp('Function 1 with no arguments.');
      end

      function fun(obj, arg)
         disp(['Function 1 with argument: ', arg]);
      end
   end
end

In this example, "SampleClass" is a class having two functions "fun" with different input arguments.

Advantages of Overloading Functions in MATLAB

In MATLAB programming, overloading functions in class definitions provides many advantages. Some key benefits of function overloading in MATLAB class definitions are given below −

  • Function overloading improves the flexibility and reliability of the MATLAB code.

  • It enhances the code readability, as it allows to use the same name for multiple functions.

  • It provides specialized implementations for different situations.

  • Function overloading promotes the code reusability.

After getting an overview of function overloading in MATLAB. Let us now understand its implementation with the help of examples.

How to Overload Functions in Class Definitions in MATLAB?

In MATLAB, we can create a class with overload function by following the steps given below −

  • Step 1 − Define a custom class.

  • Step 2 − To overload a function, define a new function with the same name and different input parameters or outputs.

  • Step 3 − Now, create objects of the custom class and use the overloaded function.

Example

Let us take some examples to understand the implementation of these steps.

% MATLAB code for function overloading 
classdef tutorial
   methods
      % Overloaded function with no input arguments
      function course(~)
         disp('This is a free tutorial course.');
      end

      % Overloaded method with one input argument
      function course(~, course_name)
         fprintf('This tutorial is for the course: %s.
', course_name); end end end

This class definition represents a class with name "tutorial" and overload functions with name "course".

Now, let us create an object of the "tutorial" class and use the overload functions.

% Create an object of tutorial class
T = tutorial();

% Call the course function with no input arguments
T.course();

% Call the course function with an argument 'MATLAB Fundamentals'
T.course('MATLAB Fundamentals');

Output

It will produce the following output −

This is a free tutorial course.
This tutorial is for the course: MATLAB Fundamentals.

Explanation

In this example, firstly we create an object "T" of the class "tutorial". Then, we call the "course" function on the object "T".

In the first case, we call the "T.course()" function with no input arguments, hence it displays a result "This is a free tutorial course.".

In the second case, we call the "T.course('MATLAB Fundamentals')" function with one input argument. It displays a result "This tutorial is for the course: MATLAB Fundamentals.".

Conclusion

We can overload a function in class definitions in MATLAB, where we define multiple functions with the same name and different input arguments. In this article, I explained the process of overloading functions in class definitions in MATLAB using an example for better understanding of the concept.

Updated on: 25-Oct-2023

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements