Method Overloading in Java is a concept related to Object Oriented Programming (OOP). Java supports overloading of methods and can distinguish between different methods with method signatures. A situation, wherein, in the same class there are two or more methods with same name, having different functions or different parameters, it is called Method Overloading. To take an in depth look at what Method Overloading is to have a better understanding of this concept.
A method is a collection of statements that are grouped together to perform an operation. It takes some parameters, performs some computations, and then optionally returns a value (or object). To create a particular functionality, a method is needed to be created so as to modularize the application. By making a method, one can call that particular method at different instances. In this way, the code redundancy can be reduced and the code can be reused. There are five components in a method:
- Modifiers– There can be four types of modifiers – Public, Private, Protected, and Default (uncategorized) modifiers.
- The return type– A method can return a value, which could be any one of integer, string, character, float or nothing. In case, a method is not returning anything, ‘void’ is needed to be mentioned in the place of return type, which will show that the method will not return anything. A method, which is not returning anything, or has a void return type, is known as procedure. If a method is returning a value, it is termed as function.
- The method name– It’s the identifier of a method. Using the name of the method, one can use that particular method.
- The parameter list in parenthesis– It is the list of arguments. If a method comprises of certain values, which it needs to compare, e.g. for performing addition of two numbers, a method will require two number as parameters. In this case, you need to pass int a, int b as the parameters.
- The method body enclosed with braces– In body, you need to provide the definition of the operation that has to be performed. The functionality of the method is presented in the body, enclosed in curly braces.
Now, that we have an idea of what a method is, let’s go back to Method Overloading that we defined in the beginning of this post – It is a situation, wherein, in the same class there are two or more methods with same name, having different functions or different parameters. Following are the conditions for method overloading in Java:
- The overloaded function must differ either by the number of arguments or operands or data types.
- The same function name is used for various instances of function call.
Method Overloading can be done in two ways:
- By changing Arguments’ data types
- By changing number of Arguments
Why Method Overloading?
Using Method Overloading in Java is very common among Java programmers, because it:
- Provides flexibility to call similar method for different data types
- Saves memory
- Saves time
- Increases consistency
- Improves code readability.
Got a question for us? Mention them in the comments section and we will get back to you.