- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Method Overloading based on the order of the arguments in Java
In method overloading, the class can have multiple methods with the same name but the parameter list of the methods should not be the same. One way to make sure that the parameter list is different is to change the order of the arguments in the methods.
A program that demonstrates this is given as follows −
Example
class PrintValues { public void print(int val1, char val2) { System.out.println("
The int value is: " + val1); System.out.println("The char value is: " + val2); } public void print(char val1, int val2) { System.out.println("
The char value is: " + val1); System.out.println("The int value is: " + val2); } } public class Demo { public static void main(String[] args) { PrintValues obj = new PrintValues(); obj.print(15, 'A'); obj.print('P', 4); } }
Output
The int value is: 15 The char value is: A The char value is: P The int value is: 4
Now let us understand the above program.
The PrintValues class is created with two methods print() in the implementation of method overloading. The first of these takes 2 parameters of type int and type char respectively and the other takes 2 parameters of type char and type int respectively. A code snippet which demonstrates this is as follows −
class PrintValues { public void print(int val1, char val2) { System.out.println("
The int value is: " + val1); System.out.println("The char value is: " + val2); } public void print(char val1, int val2) { System.out.println("
The char value is: " + val1); System.out.println("The int value is: " + val2); } }
In the main() method, object obj of class PrintValues is created and the print() method is called two times with parameters (15, ’A’) and (‘P’, 4) respectively. A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String[] args) { PrintValues obj = new PrintValues(); obj.print(15, 'A'); obj.print('P', 4); } }
- Related Articles
- What are the restrictions placed on method overloading in java?
- Method overloading in Java
- Using Method Overloading in Java
- What is method overloading in Java?
- What is the difference between method overloading and method hiding in Java?
- Difference between Method Overloading and Method Overriding in Java
- method overloading and type promotion in Java
- Method Overloading and null error in Java
- Why is method overloading not possible by changing the return type of the method only in java?
- Method Overloading and Ambiguity in Varargs in Java
- Method overloading with autoboxing and widening in Java.
- How to change the order of a matrix in increasing order based on a single column?
- Java Program to Find Area of circle Using Method Overloading
- Java Program to Find Area of Rectangle Using Method Overloading
- Java Program to Find Area of Square Using Method Overloading
