JAVA OVERLOADING METHODS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

JAVA OVERLOADING METHODS

Can you, please, show me as simple as possible examples of overloading with explanation? Thank you in advance!

4th Jun 2018, 8:28 PM
DIY Mods
DIY Mods - avatar
7 Answers
+ 3
https://beginnersbook.com/2013/05/method-overloading/ so many good example are provide there.
4th Jun 2018, 9:25 PM
Arun Tomar
Arun Tomar - avatar
+ 1
In simple word - overloading is that the methods or constructors have the same name but different number of parameter, or type of parameter, or both - different type and number. that's all.
4th Jun 2018, 10:01 PM
Krzysztof303
Krzysztof303 - avatar
+ 1
Look here https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html there is a Math class..Find a min or max methods. there is overloading by type: int, long, float, double. there are static methods so you can call them in main method by Math.min(num1, num2) and give to num1, num2 different type variables, and you have overloading
4th Jun 2018, 10:11 PM
Krzysztof303
Krzysztof303 - avatar
+ 1
or you can declare you own overloading like that: public class SomeClass{ public SomeClass(Int i){ //some code } public SomeClass(Int i, Int j){ //some other code } } and you have constructor overloading
4th Jun 2018, 10:17 PM
Krzysztof303
Krzysztof303 - avatar
0
Thank you, Krzysztof303! I understand what is it and how it works overall, but I'm trying to find as simple as possible program that uses two types of overloading and calling them both from the main
4th Jun 2018, 10:06 PM
DIY Mods
DIY Mods - avatar
0
Thanks, Arun Tomar! I've been on that page many times but didn't found what I looking for. I need a practical use of overloading in the simple program.
4th Jun 2018, 10:12 PM
DIY Mods
DIY Mods - avatar
0
If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. Method overloading increases the readability of the program. Read more on ' method overloading' here:- http://crbtech.in/Java-Training/method-overloading-overriding-java/
5th Jun 2018, 5:00 AM
pranit patil
pranit patil - avatar