[Go to site: main page, start]

0% found this document useful (0 votes)
4 views7 pages

Java Method Overloading Explained

Java method overloading allows a class to have multiple methods with the same name but different parameters, enhancing code readability and enabling compile-time polymorphism. It can be implemented by varying the number or type of arguments, but cannot be achieved by changing only the return type or by mixing static and non-static methods. Examples are provided to illustrate method overloading with both static and non-static methods in a Calculator class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Method Overloading Explained

Java method overloading allows a class to have multiple methods with the same name but different parameters, enhancing code readability and enabling compile-time polymorphism. It can be implemented by varying the number or type of arguments, but cannot be achieved by changing only the return type or by mixing static and non-static methods. Examples are provided to illustrate method overloading with both static and non-static methods in a Calculator class.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Page 1 of 7

Home Whiteboard Online Compilers Practice Articles Tools

Chapters Categories

Java - Method Overloading

Java Method Overloading


When a class has two or more methods by the same name but different parameters, at
the time of calling based on the parameters passed respective method is called (or
respective method body will be bonded with the calling line dynamically). This mechanism is
known as method overloading.

Advertisement

Advantage of Method Overloading


Method overloading improves the code readability and reduces code redundancy. Method
overloading also helps to achieve compile-time polymorphism.

Example of Method Overloading


If you observe the following example, Here we have created a class named Tester this
class has two methods with same name (add) and return type, the only difference is the

[Link] w w .[Link]/java/java_method_overloading.htm 1/7


Page 2 of 7

parameters they accept (one method accepts two integer variables and other accepts
three integer variables).

class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}

When you invoke the add() method based on the parameters you pass respective method
body gets executed.

int result = [Link](1,2); // returns 3;


result = [Link](1,2,3); // returns 6;

Different Ways of Java Method Overloading


Method overloading can be achieved using following ways while having same name
methods in a class.

Use different number of arguments

Use different type of arguments

Invalid Ways of Java Method Overloading


Method overloading cannot be achieved using following ways while having same name
methods in a class. Compiler will complain of duplicate method presence.

Using different return type

Using static and non-static methods

Method Overloading: Different Number of Arguments


You can implement method overloading based on the different number of arguments.

Example: Different Number of Arguments (Static Methods)

[Link] w w .[Link]/java/java_method_overloading.htm 2/7


Page 3 of 7

In this example, we've created a Calculator class having two static methods with same
name but different arguments to add two and three int values respectively. In main()
method, we're calling these methods and printing the result. Based on the type of
arguments passed, compiler decides the method to be called and result is printed
accordingly.

package [Link];

class Calculator{
public static int add(int a, int b){
return a + b;
}
public static int add(int a, int b, int c){
return a + b + c;
}
}

public class Tester {


public static void main(String args[]){
[Link]([Link](20, 40));
[Link]([Link](40, 50, 60));
}
}

Output

60
150

Example: Different Number of Arguments (Non Static Methods)

In this example, we've created a Calculator class having two non-static methods with
same name but different arguments to add two and three int values respectively. In main()
method, we're calling these methods using object of Calculator class and printing the
result. Based on the number of arguments passed, compiler decides the method to be
called and result is printed accordingly.

[Link] w w .[Link]/java/java_method_overloading.htm 3/7


Page 4 of 7

package [Link];

class Calculator{
public int add(int a, int b){
return a + b;
}
public int add(int a, int b, int c){
return a + b + c;
}
}

public class Tester {


public static void main(String args[]){
Calculator calculator = new Calculator();
[Link]([Link](20, 40));
[Link]([Link](40, 50, 60));
}
}

Output

60
150

Method Overloading: Different Type of Arguments


You can implement method overloading based on the different type of arguments.

Example: Different Type of Arguments

In this example, we've created a Calculator class having two non-static methods with
same name but different types of arguments to add two int values and two double values
respectively. In main() method, we're calling these methods using object of Calculator
class and printing the result. Based on the type of arguments passed, compiler decides the
method to be called and result is printed accordingly.

[Link] w w .[Link]/java/java_method_overloading.htm 4/7


Page 5 of 7

package [Link];

class Calculator{
public int add(int a, int b){
return a + b;
}
public double add(double a, double b){
return a + b;
}
}

public class Tester {


public static void main(String args[]){
Calculator calculator = new Calculator();
[Link]([Link](20, 40));
[Link]([Link](20.0, 40.0));
}
}

Output

60
60.0

TOP TUTORIALS

Python Tutorial

Java Tutorial

C++ Tutorial
C Programming Tutorial

C# Tutorial

PHP Tutorial
R Tutorial

HTML Tutorial

CSS Tutorial
JavaScript Tutorial

[Link] w w .[Link]/java/java_method_overloading.htm 5/7


Page 6 of 7

SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial

Amazon Web Services Tutorial

Microsoft Azure Tutorial


Git Tutorial

Ethical Hacking Tutorial

Docker Tutorial
Kubernetes Tutorial

DSA Tutorial

Spring Boot Tutorial


SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification


Java & Spring Boot Advanced Certification
Data Science Advanced Certification

Cloud Computing And DevOps


Advanced Certification In Business Analytics
Artificial Intelligence And Machine Learning

DevOps Certification
Game Development Certification
Front-End Developer Certification

AWS Certification Training


Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler


Online Python Compiler

Online Go Compiler
Online C Compiler
Online C++ Compiler

Online C# Compiler
Online PHP Compiler

[Link] w w .[Link]/java/java_method_overloading.htm 6/7


Page 7 of 7

Online MATLAB Compiler


Online Bash Terminal
Online SQL Compiler

Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

[Link] w w .[Link]/java/java_method_overloading.htm 7/7

You might also like