1 00:00:01 --> 00:00:05 Welcome to the Spoken Tutorial on method overloading in java. 2 00:00:06 --> 00:00:07 In this tutorial we will learn: 3 00:00:08 --> 00:00:09 * What is method overloading. 4 00:00:10 --> 00:00:12 * And to overload method. 5 00:00:13 --> 00:00:23 Here we are using: Ubuntu version 11.10 OS Java Development kit 1.6 And Eclipse 3.7.0 6 00:00:24 --> 00:00:25 To follow this tutorial, you must know 7 00:00:26 --> 00:00:28 how to create methods and 8 00:00:29 --> 00:00:31 to overload constructor in java using eclipse. 9 00:00:32 --> 00:00:38 If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org 10 00:00:39 --> 00:00:41 What is method overloading? 11 00:00:42 --> 00:00:45 Define two or more methods with same name within a class. 12 00:00:46 --> 00:00:49 They must differ in number or types of parameters. 13 00:00:50 --> 00:00:53 These methods are called overloaded methods. 14 00:00:54 --> 00:00:56 The process is called method overloading. 15 00:00:57 --> 00:00:59 Let us now see how to overload a method. 16 00:01:00 --> 00:01:05 In eclipse, I have a class Addition. 17 00:01:06 --> 00:01:09 Inside the class, we will declare two integer variables. 18 00:01:10 --> 00:01:18 So, type: int a is equal to 10 and int b is equal to 5. 19 00:01:19 --> 00:01:22 Let us create a method to add these two integers. 20 00:01:23 --> 00:01:29 So, type: void add parentheses. 21 00:01:30 --> 00:01:39 Within curly brackets, type: System dot out dot println. 22 00:01:40 --> 00:01:43 Inside parentheses a+b. 23 00:01:44 --> 00:01:49 So, this method will give us the sum of two integer variables. 24 00:01:50 --> 00:01:54 Let us create another method which takes two parameters. 25 00:01:55 --> 00:02:03 So, type: void addTwoNumbers. 26 00:02:04 --> 00:02:13 Within parentheses int num1 comma int num2. 27 00:02:14 --> 00:02:34 Then, within curly brackets System dot out dot println num1 plus num2. 28 00:02:35 --> 00:02:43 So, this method will give us the sum of two values that are passed as arguments to this method. 29 00:02:44 --> 00:02:48 Let us create an object of the class and call the methods. 30 00:02:49 --> 00:03:12 So, in the Main method type: Addition i.e the class name obj is equal to new Addition parentheses semicolon. 31 00:03:13 --> 00:03:17 Then Obj.add 32 00:03:18 --> 00:03:30 And Obj.addTwonumbers within parentheses. 33 00:03:31 --> 00:03:32 we will pass two arguments. 34 00:03:33 --> 00:03:36 Suppose if we pass floating point values. 35 00:03:37 --> 00:03:44 So, type 2.5 comma and an integer 3. 36 00:03:45 --> 00:03:56 We get an error which states that: the method addTwoNumbers int comma int of the class addition is not applicable for the arguments double comma int. 37 00:03:57 --> 00:04:05 So, what we do is, in the method instead of int we will give double. 38 00:04:06 --> 00:04:11 So replace int by double . Save the file. 39 00:04:12 --> 00:04:16 we see that the error is resolved. 40 00:04:17 --> 00:04:23 we also know that Java automatically i.e. implicitly coverts int into double. 41 00:04:24 --> 00:04:27 So, here we can pass integer arguments as well. 42 00:04:28 --> 00:04:31 Now Save and Run the program. 43 00:04:32 --> 00:04:36 In the output, we see the sum of two integer variables 44 00:04:37 --> 00:04:42 and the sum of two numeric arguments that we passed. 45 00:04:43 --> 00:04:49 Now we see that both the methods perform same operation. 46 00:04:50 --> 00:04:59 The difference is that the first method has no parameter while the second method has parameters. 47 00:05:00 --> 00:05:04 So, in such cases java provides us with method overloading. 48 00:05:05 --> 00:05:08 So, what we do is, give same name to both the methods. 49 00:05:09 --> 00:05:28 So, replace addTwoNumbers with add; also change here. 50 00:05:29 --> 00:05:32 We will define one more method with same operation. 51 00:05:33 --> 00:05:37 So, type: void add. 52 00:05:38 --> 00:05:50 And within parentheses int n1 comma int n2 comma int n3. 53 00:05:51 --> 00:05:53 So we have given three parameters. 54 00:05:54 --> 00:06:02 Then, within curly brackets System dot out dot println. 55 00:06:03 --> 00:06:10 Within parentheses n1 plus n2 plus n3. 56 00:06:11 --> 00:06:16 So, this method will give sum of three numbers. 57 00:06:17 --> 00:06:18 Let us call this method. 58 00:06:19 --> 00:06:34 So, type: obj dot add 1 comma 5 comma 4. 59 00:06:35 --> 00:06:46 Save and Run. 60 00:06:47 --> 00:06:51 So, Java compiler overloads the proper method depending on the parameters. 61 00:06:52 --> 00:06:56 It checks the number and type of parameters passed. 62 00:06:57 --> 00:07:00 So, as a programmer we don't have to worry about the method name. 63 00:07:01 --> 00:07:04 Nor even the type or number of arguments we passed. 64 00:07:05 --> 00:07:10 We can create one more method which appends strings. 65 00:07:11 --> 00:07:14 So we will create one more overload method. 66 00:07:15 --> 00:07:28 Type: void add String s1 comma String s2. 67 00:07:29 --> 00:07:40 Within curly brackets System dot out dot println. 68 00:07:41 --> 00:07:44 Within parentheses s1 plus s2. 69 00:07:45 --> 00:07:49 And we will call this method. 70 00:07:50 --> 00:07:54 So, type: obj dot add. 71 00:07:55 --> 00:08:06 Within parentheses in double quotes Hello comma in double quotes space World. 72 00:08:07 --> 00:08:11 Now Save and Run the program. 73 00:08:12 --> 00:08:15 So, in the output we see Hello space World. 74 00:08:16 --> 00:08:20 So, the add method with two string arguments, appends the string. 75 00:08:21 --> 00:08:26 Suppose now we declare add method with return type. 76 00:08:27 --> 00:08:39 So, type: int add parentheses no parameter and curly brackets. 77 00:08:40 --> 00:08:47 We get an error. It states that duplicate method add in type addition. 78 00:08:48 --> 00:08:53 This is because we have already declared a method add with no parameters. 79 00:08:54 --> 00:08:57 So, remember that to overload a method, the parameters must differ. 80 00:08:58 --> 00:09:02 Having different return types will not overload the method. 81 00:09:03 --> 00:09:08 So, remove this method and Save the file. 82 00:09:09 --> 00:09:15 This is how method overloading is done in java. 83 00:09:16 --> 00:09:17 So, in this tutorial we learnt: 84 00:09:18 --> 00:09:19 * About method overloading. 85 00:09:20 --> 00:09:21 * To overload method. 86 00:09:22 --> 00:09:24 * And advantage of method overloading. 87 00:09:25 --> 00:09:30 For self assessment: create a method subtract that subtracts number, 88 00:09:31 --> 00:09:32 overload it. 89 00:09:33 --> 00:09:35 To know more about the Spoken Tutorial project, 90 00:09:36 --> 00:09:41 watch the video available at [1] 91 00:09:42 --> 00:09:44 It summarizes the Spoken Tutorial project. 92 00:09:45 --> 00:09:47 If you do not have good bandwidth, you can download and watch it. 93 00:09:48 --> 00:09:49 The Spoken Tutorial project team: 94 00:09:50 --> 00:09:51 Conducts workshops using spoken tutorials. 95 00:09:52 --> 00:09:55 Gives certificates to those who pass an online test. 96 00:09:56 --> 00:10:00 For more details, please write to contact@spoken-tutorial.org 97 00:10:01 --> 00:10:04 Spoken Tutorial Project is a part of the Talk to a Teacher project. 98 00:10:05 --> 00:10:10 It is supported by the National Mission on Education through ICT, MHRD, Government of India. 99 00:10:11 --> 00:10:18 More information on this mission is available at [2]. 100 00:10:19 --> 00:10:20 We have come to the end of this tutorial. 101 00:10:21 --> 00:10:21 Thanks for joining. 102 00:10:22 --> 00:10:27 This is Prathamesh Salunke, signing off. Jai Hind.