What is method overloading in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 15

What is method overloading in java?

8th Mar 2017, 8:50 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
8 Answers
+ 18
say you have a method that does one thing...it takes parameters of the same type and has a return type of the same type.overloading is evident when you take the same method and Change the return type and the data type of the parameters to suit your needs.keeping in mind that the method is the same. take this as an example: //original method public int area(int a,int b){ area = a * b; return area; } //overloaded method public double area(double a,double b){ area = a * b; return area; }
8th Mar 2017, 11:19 PM
Samuel Martins
Samuel Martins - avatar
+ 16
Methods with same name but different parameter lists. Parameter lists may differ in the following ways: 1. data types int result (int a) int result (double a) 2. order of data types int result (int a, double b) int result (double b, int a) 3. number of parameters int result (int a) int result (int a, int b)
9th Mar 2017, 10:56 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 15
thank you Shamima, Mirko, Samuel
25th Mar 2017, 1:29 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 14
thank you Tashi!
8th Mar 2017, 9:33 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 14
Thank you Syeda...super helpful C++ is helpful for Java
25th Mar 2017, 1:26 AM
‎‏‎‏‎Joe
‎‏‎‏‎Joe - avatar
+ 13
I've got a simple example at the playground: https://code.sololearn.com/cJmmki0zu8Fv/?ref=app I hope that will help better than a definition ^^ Tell me if you have further questions.
8th Mar 2017, 8:53 AM
Tashi N
Tashi N - avatar
+ 5
Overloading Operator..... C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. An overloaded declaration is a declaration that had been declared with the same name as a previously declared declaration in the same scope, except that both declarations have different arguments and obviously different definition (implementation). When you call an overloaded function or operator, the compiler determines the most appropriate definition to use by comparing the argument types you used to call the function or operator with the parameter types specified in the definitions. The process of selecting the most appropriate overloaded function or operator is called overload resolution. *************************************************************************** Function overloading in C++: You can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list. You can not overload function declarations that differ only by return type. Following is the example where same function print() is being used to print different data types: #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "Printing int: " << i << endl; } void print(double f) { cout << "Printing float: " << f << endl; } void print(char* c) { cout << "Printing character: " << c << endl; } }; int main(void) { printData pd; // Call print to print integer pd.print(5); // Call print to print float pd.print(500.263); // Call print to print character pd.print("Hello C++"); return 0; } When the above code is compiled and executed, it produces the following result: Printing int: 5 Printing float: 500.263 Prin
25th Mar 2017, 12:31 AM
Syeda Juveria Afreen
Syeda Juveria Afreen - avatar
+ 4
you can have more than 1 method with the same name but with different parameters, ie: ~public string name(int a){} ~public string name(inta, int b){}
8th Mar 2017, 8:57 AM
Mirko
Mirko - avatar