Java – Method Overloading with Example2 min read

Method Overloading is one of the features in a class having more than one method with the same name. It is the same as Constructor Overloading in java. The methods are differed by their input parameter that is the data type, the number of parameters, and the order of the parameter list.

Note that: Method overloading is not possible by changing the return type of methods.

Now let us see the different ways of overloading methods.

There are two different ways in which the method can be overloaded:

  1. Method overloading, by changing the data type of Arguments.
  2. Method overloading, by changing the number of the argument.

1. Method overloading by changing the data type of Arguments.

In this method, the user needs to change the data-type of passing argument for two different methods of the same name and the parameter list must match with that data type accordingly.

The following program shows the example of using Method overloading by changing the data type of Arguments in Java:

The output of Method overloading:

sum is 10
sum is 11.0


2. Method overloading by changing the number of the argument.

Another way is by changing the number of arguments that is the number of parameters passed when called. We can also keep the argument empty and it will recognize the method with an empty parameter list.

Following is the example method overloading by changing the number of arguments in Java:

The output of Method overloading:

Argument List is empty

Name:Karan Id:1101

Name:Prashant Id:1102 Age:23


MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …