- 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
Why is method overloading not possible by changing the return type of the method only in java?
Overloading is the mechanism of binding the method call with the method body dynamically based on the parameters passed to the method call.
If you observe the following example, it contains two methods with same name, different parameters and if you call the method by passing two integer values the first method will be executed and, if you call by passing 3 integer values then the second method will be executed.
It is not possible to decide to execute which method based on the return type, therefore, overloading is not possible just by changing the return type of the method.
Example
public class Sample{ public int add(int a, int b){ int c = a+b; return c; } public void add(int a, int b, int c){ int z = a+b+c; System.out.println(z); } public static void main(String args[] ){ Sample obj = new Sample(); System.out.println(obj.add(40, 50)); obj.add(40, 50, 60); } }
Output
90 150
- Related Articles
- method overloading and type promotion in Java
- Why is operator overloading not supported by java?
- What is method overloading in Java?
- Method overloading in Java
- What is the difference between method overloading and method hiding in Java?
- What is the difference between method overloading and method overriding in Java?
- Using Method Overloading in Java
- Method overloading v/s method overriding in Java
- Method Overloading based on the order of the arguments in Java
- Can we change return type of main() method in java?
- If I change the return type, will the method gets overloaded in java?
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- Method Overloading and null error in Java
- What are the restrictions placed on method overloading in java?
- What is method overloading in C#?

Advertisements