|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Method Overloading In Java" |
| 4 | +author: gaurav |
| 5 | +categories: [Java, Java Interview Questions] |
| 6 | +toc: false |
| 7 | +description: "In this tutorial, we will see the method overloading in Java." |
| 8 | +--- |
| 9 | + |
| 10 | +In this tutorial, we will see the method overloading in Java. |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +It is known as **Method Overloading** when a class has the same name but a different method signature. |
| 15 | + |
| 16 | +For example, when we have a method `add()`, we can have it in multiple forms. |
| 17 | + |
| 18 | +For example `add(int a, int b)` or `add(int a, int b, int c)` or `add(double a, double b)` |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +> **What Is Method Signature In Java** |
| 23 | +> |
| 24 | +> In Java, method signature contains method name and type of arguments. |
| 25 | +> |
| 26 | +> <method-name>(<argument-type-1>, <argument-type-2>...) |
| 27 | +> |
| 28 | +> Ex. `getData(int, String)` |
| 29 | +> |
| 30 | +> **Note:** In java, the method signature does not include the name of parameter, it only includes it's datatype. |
| 31 | +
|
| 32 | +--- |
| 33 | + |
| 34 | +Compile-time polymorphism can be achieved by Method overloading. |
| 35 | + |
| 36 | +In Java, there are two ways to overload the method. |
| 37 | + |
| 38 | +1. By changing number of arguments |
| 39 | +2. By changing the data type of arguments |
| 40 | + |
| 41 | +## 1. By changing number of arguments |
| 42 | + |
| 43 | +We can overload a method **by changing the number of arguments i.e parameters it receives**. |
| 44 | + |
| 45 | +As shown in the example from the introduction, we can overload the method `add(int a, int b)` by making the parameters three. i.e `add(int a, int b, int c)` |
| 46 | + |
| 47 | +```java |
| 48 | +public class Operation{ |
| 49 | + |
| 50 | + public static int add(int a, int b){ |
| 51 | + return a+b; |
| 52 | + } |
| 53 | + |
| 54 | + public static int add(int a, int b, int c){ |
| 55 | + return a+b+c; |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +## 2. By changing the data type of arguments |
| 62 | + |
| 63 | +We can overload a method **by changing the data type of the arguments i.e parameters it receives**. |
| 64 | + |
| 65 | +As shown in the example from the introduction, we can overload the method `add(int a, int b)` by changing the data type of the parameters. i.e `add(double a, double b)` |
| 66 | + |
| 67 | +```java |
| 68 | +public class Operation{ |
| 69 | + |
| 70 | + public static int add(int a, int b){ |
| 71 | + return a+b; |
| 72 | + } |
| 73 | + |
| 74 | + public static double add(double a, double b){ |
| 75 | + return a+b; |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Method overloading improves the readability of the code. |
| 82 | + |
| 83 | +## Conclusion |
| 84 | + |
| 85 | +It is known as **Method Overloading** when a class has the same name but a different method signature. |
| 86 | + |
| 87 | +We can overload the method by changing the number of arguments or changing the data type of arguments. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +Please write your thoughts in the comment section below. |
0 commit comments