Method Overloading in Java
Method Overloading in Java: An Introduction
Java is an object-oriented programming language. Objects are the core of Java programming that exhibit specific properties, like real-world objects. Generally, each method is associated with particular properties.
Objects and Methods:
OOPs refers to object-oriented programming language where the programming code uses objects as the primary criterion or building blocks instead of functions or methods. Several programming languages including Java, Python, and C++ use objects as building blocks.
In procedural programming such as C programming, methods or procedures are responsible for processing the code and performing logical operations on the given code.
However, there is a chance of repeating specific methods with similar properties, which is known as overloading in Java.
If a method redefines in the same class more than once, it is known as method overloading in Java. However, those methods differ in their parameters.
Object-oriented programming languages such as Java will exhibit overloading features in their programs. In contrast, C programming language, a structured programming language, cannot express features such as overloading.
Overloading in Java:
We will take the example of a calculator to understand the concept of overloading in Java.
Though the method is repeated, it should differ in three different aspects. They include,
1) The overloading in Java must differ in the number of arguments or parameters.
For instance, if the first method contains only one argument that includes the ‘int’.
The second method should contain two arguments: int and float.
2) Secondly, the method should also differ by type of arguments or parameters.
For instance, if the first method contains two arguments or parameters, the second also includes two but should differ by parameter type.
They include the following,
The first method contains int and float parameters, and the second methods contain float and double.
3) Third, the method with similar properties should vary by the order of arguments or parameters.
For instance, if the first method contains three parameters in int, double, and float, the second should include the parameters in a different order, like double, float, and int.