Scanner class Methods in Java

Java - Method Overloading

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.


Method overloading example in java

In the example above,

  1. The class "MethodOverloadingSumNum" is created.
  2. 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.
  3. First method "sum(int a,int b)" is used to print the sum of integers.
  4. Second method "sum(double a, double b) " is used to print sum of two double type numbers.
  5. Third method "sum(char a,char b)" is used to print the sum of integers.
  6. Fourth method "sum(String e a, String b) " is used to print sum of two double type numbers.
  7. All method have only one line of code.A print statement to print sum.
  8. In the main method, we created a object of class to access methods. Then using obj all methods are invoked with dot operator.
  9. 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.
  10. Lets see the output.
Method overloading example
Its working perfectly fine.Try this program on your own. Here, we used String datatype to add two strings. In the output two different strings are presented like one single string.

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

  1. We only need to remember one name for different methods used in program to perform similar operations.
  2. Maintaining program becomes easy.
  3. Reduced complexity of programs.
  4. Increase readability of programs.
  5. 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.

Write a program to calculate the area with different number of parameters using Method Overloading.
In the example above,
  1. We have defined three methods "area". All methods have same dataype "int" for parameters.The only difference is in the number of arguments.
  2. First method calculates the area of square hence only have one parameter.
  3. Seconds finds area of rectangle so, having two parameters.
  4. Third calculates area of triangle and containing three parameters for that.
  5. In main method, each method is invoked using object "obj" of class "CalculateArea".
Output: Have a look at result.

Write a program to calculate the area with different number of parameters using Method Overloading.

If you like the post, let me know by commenting or you can ask any doubt in the comment section.

Happy Coding!😊



 

Comments