

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
If I change the return type, will the method gets overloaded 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.
Example
class Test{ public int addition(int a, int b){ int result = a+b; return result; } public int addition (int a, int b, int c){ int result = a+b+c; return result; } public static void main(String args[]){ Test t = new Test(); System.out.println(t.addition(25, 36)); System.out.println(t.addition(25, 50, 25)); } }
Output
61 100
Overloading based on different return type
In overloading it is must that the both methods have −
- same name.
- different parameters (different type or, different number or both).
Same return type is not mandatory. Therefore, you can overload methods with different return types if they have same name and different return types.
Example
In the following example we are trying to overload two methods: They have same name (addition) different parameters and different return types.
class Test{ public int addition(int a, int b){ int result = a+b; return result; } public float addition (float a, float b){ float result = a+b; return result; } public static void main(String args[]){ Test t = new Test(); System.out.println(t.addition(25, 36)); System.out.println(t.addition(100.25f, 36.1f)); } }
Output
61 136.35
- Related Questions & Answers
- Can we change return type of main() method in java?
- What will happen if we directly call the run() method in Java?
- What will MySQL CHAR_LENGTH() function return if I provide NULL to it?
- Why is method overloading not possible by changing the return type of the method only in java?
- Pass long parameter to an overloaded method in Java
- What will happen if the MySQL AUTO_INCREMENT column reaches the upper limit of the data type?
- Covariant return type in Java
- Overloaded method and ambiguity in C#
- What is the return type of a Constructor in Java?
- Return the cumulative product treating NaNs as one but change the type of result in Python
- Importance of return type in Java?
- Will a finally block execute after a return statement in a method in Java?
- What happens if I will prepare the statement with the same name without de-allocating the earlier one?
- Can the main method in Java return a value?
- What is covariant return type in Java?
Advertisements