- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is method overloading in Java?
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.
If you observe the following example, Here we have created a class named Sample and this class has two methods with same name (add) and return type, the only difference is the parameters they accept (one method accepts two integer variables and other accepts three integer variables).
When you invoke the add() method based on the parameters you pass respective method body gets executed.
Example
public class Sample{ public static void add(int a, int b){ System.out.println(a+b); } public static void add(int a, int b, int c){ System.out.println(a+b+c); } public static void main(String args[]){ Sample obj = new Sample(); obj.add(20, 40); obj.add(40, 50, 60); } }
Output
60 150
- Related Articles
- Method overloading in Java
- What is the difference between method overloading and method hiding in Java?
- What is method overloading in C#?
- What is method overloading in PHP?
- What is Overloading in Java?
- Using Method Overloading in Java
- What is overloading? What happens if we overload a main method in java?
- What is constructor overloading in Java?
- What are the restrictions placed on method overloading in java?
- Method Overloading and null error in Java
- method overloading and type promotion in Java
- Difference between Method Overloading and Method Overriding in Java
- Method Overloading and Ambiguity in Varargs in Java
- What is runtime polymorphism or dynamic method overloading?
- Method overloading with autoboxing and widening in Java.

Advertisements