|
| 1 | +# Java Tutorial: Method Overloading in Java |
| 2 | +- In Java, it is possible for a class to contain two or more methods with the same name but with different parameters. Such methods are called Overloaded methods. |
| 3 | +- Method overloading is used to increase the readability of the program. |
| 4 | +``` |
| 5 | +void foo() |
| 6 | +void foo(int a) //Overloaded function foo |
| 7 | +int foo(int a, int b) |
| 8 | +``` |
| 9 | + |
| 10 | +### Ways to perform method overloading : |
| 11 | +- In Java, method overloading can be performed by two ways listed below : |
| 12 | +1.By changing the return type of the different methods |
| 13 | +2. By changing the number of arguments accepted by the method |
| 14 | + |
| 15 | +- Now, let's have an example to understand the above ways of method overloading : |
| 16 | + |
| 17 | +### 1. By changing the return type : |
| 18 | +- In the below example, we've created a class named calculate. |
| 19 | +- In the calculate class, we've two methods with the same name i.e. multiply |
| 20 | +- These two methods are overloaded because they have the same name but their return is different. |
| 21 | +- The return type of 1st method is int while the return type of the other method is double. |
| 22 | + |
| 23 | +``` |
| 24 | +class calculate{ |
| 25 | + int multiply(int a,int b){ |
| 26 | + return a*b; |
| 27 | + } |
| 28 | + double multiply(double a,double b){ |
| 29 | + return a*b; |
| 30 | + } |
| 31 | +
|
| 32 | +public static void main(String[] args) { |
| 33 | +
|
| 34 | + calculate obj = new calculate(); |
| 35 | + int c = obj.multiply(5,4); |
| 36 | + double d = obj.multiply(5.1,4.2); |
| 37 | + System.out.println("Mutiply method : returns integer : " + c); |
| 38 | + System.out.println("Mutiply method : returns double : " + d); |
| 39 | +
|
| 40 | +} |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +``` |
| 45 | +Output : |
| 46 | +Mutiply method : returns integer : 20 |
| 47 | +Mutiply method : returns double : 21.419999999999998 |
| 48 | + ``` |
| 49 | + |
| 50 | +### 2. By changing the number of arguments passed : |
| 51 | +- Again, we've created two methods with the same name i.e., multiply |
| 52 | +- The return type of both the methods is int. |
| 53 | +- But, the first method 2 arguments and the other method accepts 3 arguments. |
| 54 | +- Example : |
| 55 | + |
| 56 | +``` |
| 57 | +class calculate{ |
| 58 | + int multiply(int a,int b){ |
| 59 | + return a*b; |
| 60 | + } |
| 61 | + int multiply(int a,int b,int c){ |
| 62 | + return a*b*c; |
| 63 | + } |
| 64 | +
|
| 65 | +public static void main(String[] args) { |
| 66 | +
|
| 67 | + calculate obj = new calculate(); |
| 68 | + int c = obj.multiply(5,4); |
| 69 | + int d = obj.multiply(5,4,3); |
| 70 | + System.out.println(c); |
| 71 | + System.out.println(d); |
| 72 | +
|
| 73 | +} |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +``` |
| 78 | +Output : |
| 79 | +20 |
| 80 | +60 |
| 81 | +``` |
| 82 | + |
| 83 | +**Note:** Method overloading cannot be performed by changing the return type of methods. |
| 84 | + |
| 85 | +### Code as described in the video: |
| 86 | +``` |
| 87 | +public class cwh_32_method_overloading { |
| 88 | + static void foo(){ |
| 89 | + System.out.println("Good Morning bro!"); |
| 90 | + } |
| 91 | +
|
| 92 | + static void foo(int a){ |
| 93 | + System.out.println("Good morning " + a + " bro!"); |
| 94 | + } |
| 95 | +
|
| 96 | + static void foo(int a, int b){ |
| 97 | + System.out.println("Good morning " + a + " bro!"); |
| 98 | + System.out.println("Good morning " + b + " bro!"); |
| 99 | + } |
| 100 | +
|
| 101 | + static void foo(int a, int b, int c){ |
| 102 | + System.out.println("Good morning " + a + " bro!"); |
| 103 | + System.out.println("Good morning " + b + " bro!"); |
| 104 | + } |
| 105 | +
|
| 106 | + static void change(int a){ |
| 107 | + a = 98; |
| 108 | + } |
| 109 | +
|
| 110 | + static void change2(int [] arr){ |
| 111 | + arr[0] = 98; |
| 112 | + } |
| 113 | + static void tellJoke(){ |
| 114 | + System.out.println("I invented a new word!\n" + |
| 115 | + "Plagiarism!"); |
| 116 | + } |
| 117 | +
|
| 118 | + public static void main(String[] args) { |
| 119 | + // tellJoke(); |
| 120 | +
|
| 121 | + // Case 1: Changing the Integer |
| 122 | + //int x = 45; |
| 123 | + //change(x); |
| 124 | + //System.out.println("The value of x after running change is: " + x); |
| 125 | +
|
| 126 | + // Case 1: Changing the Array |
| 127 | + // int [] marks = {52, 73, 77, 89, 98, 94}; |
| 128 | + // change2(marks); |
| 129 | + // System.out.println("The value of x after running change is: " + marks[0]); |
| 130 | +
|
| 131 | +
|
| 132 | + // Method Overloading |
| 133 | + foo(); |
| 134 | + foo(3000); |
| 135 | + foo(3000, 4000); |
| 136 | + // Arguments are actual! |
| 137 | +
|
| 138 | +
|
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-32/Ch7.pdf)** |
| 144 | + |
| 145 | +**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-32/UltimateJavaCheatSheet.pdf)** |
0 commit comments