Open In App

Method Overloading in Java

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference can be in the number of parameters, the types of parameters, or the order of those parameters.

Method overloading in Java is also known asCompile-time Polymorphism, Static Polymorphism, or Early binding, because the decision about which method to call is made at compile time. When there are overloaded methods that accept both a parent type and a child type, and the provided argument could match either one, Java prefers the method that takes the more specific (child) type. This is because it offers a more better match.

Key features of Method Overloading:

  • Multiple methods can share the same name in a class when their parameter lists are different.
  • Overloading is a way to increase flexibility and improve the readability of code.
  • Overloading does not depend on the return type of the method, two methods cannot be overloaded by just changing the return type.

Now, let us go through a simple example to understand the concept better:

Example: This example demonstratesmethod overloading by defining multiple sum() methods with different parameter types and counts.

Java
// Java program to demonstrate working of method// overloading in JavapublicclassSum{// Overloaded sum() // This sum takes two int parameterspublicintsum(intx,inty){return(x+y);}// Overloaded sum()// This sum takes three int parameterspublicintsum(intx,inty,intz){return(x+y+z);}// Overloaded sum() // This sum takes two double parameterspublicdoublesum(doublex,doubley){return(x+y);}// Driver codepublicstaticvoidmain(Stringargs[]){Sums=newSum();System.out.println(s.sum(10,20));System.out.println(s.sum(10,20,30));System.out.println(s.sum(10.5,20.5));}}

Output
30 60 31.0 


Different Ways of Method Overloading in Java

The different ways of method overloading in Java are listed below:

  • Changing the Number of Parameters.
  • Changing Data Types of the Arguments.
  • Changing the Order of the Parameters of Methods

1. Changing the Number of Parameters

Method overloading can be achieved by changing the number of parameters while passing to different methods.

Example: This example demonstrates method overloading by changing the number of parameters in the multiply() method.

Java
// Java Program to Illustrate Method Overloading// By Changing the Number of Parameters// Importing required classesimportjava.io.*;// Class 1// Helper classclassProduct{// Method 1// Multiplying two integer valuespublicintmultiply(inta,intb){intprod=a*b;returnprod;}// Method 2// Multiplying three integer valuespublicintmultiply(inta,intb,intc){intprod=a*b*c;returnprod;}}// Class 2// Main classclassGeeks{// Main driver methodpublicstaticvoidmain(String[]args){// Creating object of above class inside main()// methodProductob=newProduct();// Calling method to Multiply 2 numbersintprod1=ob.multiply(1,2);// Printing Product of 2 numbersSystem.out.println("Product of the two integer value: "+prod1);// Calling method to multiply 3 numbersintprod2=ob.multiply(1,2,3);// Printing product of 3 numbersSystem.out.println("Product of the three integer value: "+prod2);}}

Output
Product of the two integer value: 2 Product of the three integer value: 6 


2. Changing Data Types of the Arguments

In many cases, methods can be considered Overloaded if they have the same name but have different parameter types, methods are considered to be overloaded.

Example: This example demonstrates method overloading by changing the data types of parameters in the Prod() method.

Java
// Java Program to Illustrate Method Overloading// By Changing Data Types of the Parameters// Importing required classesimportjava.io.*;// Class 1// Helper classclassProduct{// Multiplying three integer valuespublicintProd(inta,intb,intc){intprod1=a*b*c;returnprod1;}// Multiplying three double values.publicdoubleProd(doublea,doubleb,doublec){doubleprod2=a*b*c;returnprod2;}}classGeeks{publicstaticvoidmain(String[]args){Productobj=newProduct();intprod1=obj.Prod(1,2,3);System.out.println("Product of the three integer value: "+prod1);doubleprod2=obj.Prod(1.0,2.0,3.0);System.out.println("Product of the three double value: "+prod2);}}

Output
Product of the three integer value: 6 Product of the three double value: 6.0 


3. Changing the Order of the Parameters of Methods

Method overloading can also be implemented by rearranging the parameters of two or more overloaded methods. For example, if the parameters of method 1 are (String name, int roll_no) and the other method is (int roll_no, String name) but both have the same name, then these 2 methods are considered to be overloaded with different sequences of parameters.

Example: This example demonstrates method overloading by changing the order of parameters in the StudentId() method.

Java
// Java Program to Illustrate Method Overloading// By changing the Order of the Parameters// Importing required classesimportjava.io.*;// Class 1// Helper classclassStudent{// Method 1publicvoidStudentId(Stringname,introll_no){System.out.println("Name: "+name+" "+"Roll-No: "+roll_no);}// Method 2publicvoidStudentId(introll_no,Stringname){// Again printing name and id of personSystem.out.println("Roll-No: "+roll_no+" "+"Name: "+name);}}// Class 2// Main classclassGeeks{// Main functionpublicstaticvoidmain(String[]args){// Creating object of above classStudentobj=newStudent();// Passing name and id// Note: Reversing orderobj.StudentId("Sweta",1);obj.StudentId(2,"Gudly");}}

Output
Name: Sweta Roll-No: 1 Roll-No: 2 Name: Gudly 


What if the Exact Prototype Does Not Match With Arguments?

 Priority-wise, the compiler takes these steps:

  • Type Conversion but to a higher type(in terms of range) in the same family.
  • Type conversion to the next higher family (suppose if there is no long data type available for an int data type, then it will search for the float data type).


Type Conversion in Java


Let’s take an example to clarify the concept:

Java
// Demo ClassclassDemo{publicvoidshow(intx){System.out.println("In int"+x);}publicvoidshow(Strings){System.out.println("In String"+s);}publicvoidshow(byteb){System.out.println("In byte"+b);}}classUseDemo{publicstaticvoidmain(String[]args){bytea=25;Demoobj=newDemo();// it will go to// byte argumentobj.show(a);// Stringobj.show("hello");// Intobj.show(250);// Since char is// not available, so the datatype// higher than char in terms of// range is int.obj.show('A');// Stringobj.show("A");// since float datatype// is not available and so it's higher// datatype, so at this step their// will be an error.obj.show(7.5);}}


Output:

./UseDemo.java:46: error: no suitable method found for show(double)
obj.show(7.5);
^
method Demo.show(int) is not applicable
(argument mismatch; possible lossy conversion from double to int)
method Demo.show(String) is not applicable
(argument mismatch; double cannot be converted to String)
method Demo.show(byte) is not applicable
(argument mismatch; possible lossy conversion from double to byte)
1 error


Advantages of Method Overloading 

The advantages of method overlaoding is listed below:

  • Method overloading improves the Readability and reusability of the program.
  • Method overloading reduces the complexity of the program.
  • Using method overloading, programmers can perform a task efficiently and effectively.
  • Using method overloading, it is possible to access methods performing related functions with slightly different arguments and types.
  • Objects of a class can also be initialized in different ways using the constructors.


Disadvantages of Method Overloading

  • Too many overloaded methods can make the code harder to read and maintain.
  • Similar method names with different parameters can confuse developers and lead to incorrect usage.
  • Improper use may lead to ambiguity in method selection, especially with type conversions.


Method Overloading vs Method Overriding

The table below demonstrates the difference between Method Overloading and Method Overriding

Feature

Method Overloading

Method Overriding

Definition

Same method name, different parameters

Same method signature, different class

Polymorphism Type

Compile-time polymorphism (Static)

Runtime polymorphism (Dynamic)

Inheritance

No inheritance involved

Involves inheritance (parent-child)




Next Article
Article Tags :
Practice Tags :

Similar Reads

close