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