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

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More