What is method overloading in Java?
Method overloading in Java is an object-oriented programming concept that allows a programmer to declare two methods of the same name but with different method signatures, like a change in the argument list or a change in the type of argument. Method overloading is a powerful Java programming technique to declare a method that does a similar job but with a different kind of input. One of the most popular examples of method overloading is the System.out.println() method whose job is to print data on the console. This method is overloaded to accept all kinds of data types in Java.
Method overloading in Java is an object-oriented programming concept that allows a programmer to declare two methods of the same name but with different method signatures, like a change in the argument list or a change in the type of argument. Method overloading is a powerful Java programming technique to declare a method that does a similar job but with a different kind of input. One of the most popular examples of method overloading is the System.out.println() method whose job is to print data on the console. This method is overloaded to accept all kinds of data types in Java.
You have the println() method which takes String, int, float, double, or even char in output. All of those methods are collectively referred to as an overloaded method in Java.
You should also know that difference between method overloading and overriding is also a popular Java interview question. In the next section, we will some important points about method overloading in Java and then a simple example of how to overload a method in Java.
By the way, if you are new to Java and object-oriented programming then I also recommend you to join a comprehensive Java course like The Complete Java Masterclass on Udemy to learn in a more structured way. This 80-hour long course is the most updated and comprehensive and you can get it for just $10 on Udemy sales now.
2) Overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime.
Properties of method overloading in Java
1) Overloaded methods are bonded using static binding in Java. Which occurs during compile time i.e. when you compile a Java program. During the compilation process, the compiler bind method calls to the actual method.2) Overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime.
3) Most important rule of method overloading in Java is that two overloaded methods must have a different signature.
Here is an example of what does method signature means in Java:
- A number of arguments to a method are part of the method signature.
- Type of argument to a method is also part of the method signature
- Order of argument also forms part of the method signature provided they are of a different type.
- The return type of method is not part of the method signature in Java.
Method Overloading Example in Java
Here is a list of methods and there is a corresponding overloaded method with the reason that How they are overloaded :Original method :
public void show(String message){
System.out.println(message);
}
Overloaded method: number of arguments is different
public void show(String message, boolean show){
System.out.println(message);
}
Overloaded method: type of argument is different
public void show(Integer message){
System.out.println(message);
}
Not an Overloaded method: the only return type is different
public boolean show(String message){
System.out.println(message);
return false;
}
In the summary method, overloading means multiple methods with the same name but with a different signature. remember return type is not part of the method signature. method overloading is also completely different from method overriding which is a similar concept and we will see in the next article.
That's all on what is method overloading in Java, let me know if you have any questions related to How to overload a method in Java.
Thanks for reading this article so far. If you find this Java OOP tutorial and example useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.
Other Java programming and OOP tutorials You may like
- My favorite courses to learn object-oriented programming
- 5 differences between Hashtable and HashMap in Java
- 5 Free Object-Oriented programming courses for beginners
- 10 Java Coding Interview Questions and Answers for Java beginners.
- Difference between HashMap and ConcurrentHashMap in Java
- 7 best OOP design pattern courses for Java developers
- What is a factory method design pattern in Java
- Difference between TreeSet and HashSet in Java
- Top 5 Courses to Learn Design Patterns in Java
- 5 Free Data Structure and Algorithms Courses
- 5 Free Spring Framework Courses for Java Developers
- 5 Free Linux Courses for Programmers and Developers
- 5 Free Courses to Learn React JS Framework
- Top 5 Courses to Learn TensorFlow and Machine Learning
- Top 5 Courses to learn Web Development in 2020
- 10 Free Courses to learn Java for Beginners and Experienced Programmers
Thanks for reading this article so far. If you find this Java OOP tutorial and example useful then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this Object Oriented Programming (OOPs) for the JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course.
Great post. What is method overloading in Java is also a popular question for many interviews.
ReplyDeleteMethod overloading in any programming language, including Java will work fine if:
ReplyDelete- the two variants have a different number of parameters
- the two variants have the same number of parameters but the types are unrelated
In both cases, the overloaded method should logically consist of the same name and operation, but with different kind of inputs.
For example, this is fine:
LocalTime.of(int hour, int minute)
LocalTime.of(int hour, int minute, int second)
as both the method are actually creating time but with different set of input, so name of() is valid and there is on confusion between these two methods.
Here is another example, which is also fine:
Bundle.of(String[] data)
Bundle.of(Iterable data)
(there is no possible confusion between the types, the overload method is providing flexible input handling for callers, both methods must perform the same logical task and that's why same name)
But if you have the same number of parameters and related types, then it's a big problem. That should be avoided as it causes users great difficulty in figuring out which method is called
If two overloaded methods actually behave differently, then they also fail the rule that overloads should be logically equivalent.
hmm..it looks like the same parameters to,,i mean in the local time part.
ReplyDeleteany other example?
Thanks for this tutorial
ReplyDeleteYou told overloading example is System.out.println(). So can you explain different argument types in println() in detailed explanation is required.
public Double add(int a, double b) {
ReplyDeletereturn a+b;
}
public Double add(double a, int b) {
return a+b;
}
Is this method overloading or not?
yes, as sequence of arguments are interchanged
ReplyDelete