- Get link
- X
- Other Apps
Method Overloading
Java gave us liberty to use same method name for several methods. When a java class contains several methods having same names, it is know as Method overloading.But there are some conditions.The methods can have same name but their number of parameters or type of parameters must be different.
Method overloading is very helpful in writing programs which require operations of same type. We can just use the same name.Imagine a program where you need to calculate sum of different type of variables then you will have to think of different name for each method.With method overloading we can use same name making it easy to remember methods.
Method overloading is one of the frequently asked questions in interviews.
I am focused on practical learning rather than theoretical.To learn this concept first go through the example below. After the example I will elaborate the concept to you.Always practice the examples by yourself.It makes learning stimulating.
Lets understand with an example of method overloading.
Example:
Write a program to calculate sum of two numbers of different types using concept of overloading.
In the example above,
- The class "MethodOverloadingSumNum" is created.
- We defined four methods in the class below main method. Each method has same name "sum". Thy differ only in the datatypes of their arguments or parameters.
- First method "sum(int a,int b)" is used to print the sum of integers.
- Second method "sum(double a, double b) " is used to print sum of two double type numbers.
- Third method "sum(char a,char b)" is used to print the sum of integers.
- Fourth method "sum(String e a, String b) " is used to print sum of two double type numbers.
- All method have only one line of code.A print statement to print sum.
- In the main method, we created a object of class to access methods. Then using obj all methods are invoked with dot operator.
- It looks like ,we are calling same method with different arguments. This is the Method overloading. If we did not use method overloading then we would have to make four methods with different names and code will become inefficient and complex.
- Lets see the output.
Note: In Method overloading, while creating methods with same name, you must use different datatypes of parameters for each method or different numbers of parameters e.g first method having one parameters, second having three.These conditions are important because they help the java compiler to differentiate between methods. The errors are found and resolved by compiler so, Method Overloading also knows as compile-time or static Polymorphism. We will cover Polymorphism in later post. It is one of the Oops concept.
If I have used same data type in first two methods as sum(int a, int b). Then compiler would have given error stating that there are duplicate methods in program.
Advantages of Method Overloading
- We only need to remember one name for different methods used in program to perform similar operations.
- Maintaining program becomes easy.
- Reduced complexity of programs.
- Increase readability of programs.
- Overloaded methods are extensively used for handling class objects.
Important points to remember while using Method Overloading
- It is not recommended to use method overloading on methods performing different operations.As it will make program more complex and unreadable. For example, you can not apply same name to methods that are performing different operations like addition, subtraction , multiplication and division.
- If the methods differ only in the return type then they are not considered as overloaded methods and hence compiler through error.This is because compiler does not understand which method to call.
Example: This will illustrate the method overloading by using different number of parameters but type of parameters will be same.
Write a program to calculate the area with different number of parameters using Method Overloading.
In the example above,- We have defined three methods "area". All methods have same dataype "int" for parameters.The only difference is in the number of arguments.
- First method calculates the area of square hence only have one parameter.
- Seconds finds area of rectangle so, having two parameters.
- Third calculates area of triangle and containing three parameters for that.
- In main method, each method is invoked using object "obj" of class "CalculateArea".
Output: Have a look at result.
If you like the post, let me know by commenting or you can ask any doubt in the comment section.
Happy Coding!😊
Comments
Post a Comment
If you have any doubt, ask here