Method Overloading in Java [With Examples]
Java is an open-source programming language that is based on OOP principles. Object-oriented programming’s many benefits include code that is more easily maintained, understood, and fixed if bugs are found. Object-oriented programming includes several different concepts, such as inheritance, polymorphism, encapsulation, and abstraction. Method overloading, a form of polymorphism in Java, is the topic of this article.
Polymorphism: An Introduction
The ability to assume several forms is what we mean when we talk about polymorphism. In this way, the same technique can be used to demonstrate a wide variety of behaviors. Polymorphism comes in two flavors: method overloading and method overriding. When numerous methods share the same name but take distinct arguments, this is known as “method overloading.” For a method to be “overridden,” it must have the same name as one in the parent class but a distinct implementation in the child class. In this article, we will delve deeper into the topic of Method Overloading.
Different Ways to Overload a Method
The Number of Parameters
Given that each overloaded method has its own set of parameters from which the method call statement must choose, it is obvious that the names themselves provide this information. The Java code below demonstrates method overloading, in which the number of required parameters changes.
Method1: add(int, int)
Method2: add(int, int, int)
Method calling statement add(5,6) will execute the method1 and Method calling statement add(4,5,6) will execute the method2.
Changing the number of parameters:
Multiply() (Without parameter)
Multiply(int a, int b) (Multiplies with two parameters)
Multiply(int a, int b, int c) (Multiplies with three parameters)
With these three variations, we may alter the total amount of parameters for the Multiply () function. In a similar vein, you can add extra parameters to the Multiply() function so that many methods with the same name can be overridden.
The Data Type of Parameters
This is an example of Java’s method overloading, in which two methods of the same class share the same name but take different numbers and types of parameters.
Method1: add(int, int)
Method2: add(int, float)
Method3: add(float, float)
In each of the aforementioned methods, the name of the method is the same, but the data types of the parameters are different. Therefore, method2 will be executed if the method-calling expression add(8, 9.5) is used. Similarly, method3 will be carried out if you enter add(2.4, 5.8).
Changing the data type of any single or all parameters:
Multiply(int x, int y) (Has the same data type for both the parameters.)
Multiply(int x, float y) (Has a different data type.)
Multiply(int x, int y, float z) (Has a different data type.)
The final two illustrations of method overloading demonstrate the use of various data types for the parameters. To overload the ‘multiply’ function in Java, you can make such combinations of arguments.
Sequence Data Type of Parameters
The only difference between these two varieties of method overloading in Java is the order in which the data types of the parameters are specified. The method name and the total number of parameters remain the same in both variants. The following is an example of overloaded methods:
Method1: add(int, float)
Method2: add(float, int)
Here, calling the statement to add(23, 52.6) will call the method1 to execute, and add(18.36, 90) will execute method2. The parameters of the calling statement, then, determine which of the multiple overloaded methods will be carried out.
Why Do We Need Method Overloading in Java?
Now that you know how to overload methods in Java and have seen a few examples, it’s time to consider why this feature is so useful.
Java’s method overloading capability is particularly useful when a programmer has already written a method to carry out a specific action but now must modify the method so that it operates on a different set of arguments to meet a new requirement. The programmer can save themselves the trouble of remembering many names by giving similar methods the same name during creation.
Once you’ve grasped the concept of method overloading in Java, you’ll want to know why you’d ever want to. Java’s overloading feature has many applications. As long as the parameters (arguments) and/or types of the methods being overloaded are different, this feature can be used. A difficult mathematical operation may occasionally require only two or three numbers. It’s for situations like this that Java’s function overloading comes in handy.
In Java, method overloading is used when a programmer creates a method to carry out a specific action but subsequently discovers that the same method must carry out a different action on various arguments. The programmer can then write comparable methods with the same name, saving themselves the trouble of having to remember many names for the same thing.
Final Words
We have reached the final parts of the article, having successfully discussed method overloading in Java with examples. If you have a knack for programming and feel it’s a great path for a future career, Skillslash can help you get into it with its Full Stack Developer Course In Bangalore. It’s also popularly known for providing the best Data Science Course In Jaipur with a placement guarantee. Contact the support team to know more.