Difference between revisions of "Java/C2/Method-overloading/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
 
 
 
{| border=1
 
{| border=1
 
|| '''Time'''
 
|| '''Time'''
Line 8: Line 5:
 
|-
 
|-
 
|  00:01
 
|  00:01
| Welcome to the Spoken Tutorial on '''method overloading''' in''' java'''.
+
| Welcome to the Spoken Tutorial on '''Method overloading''' in''' java'''.
  
 
|-
 
|-
Line 16: Line 13:
 
|-
 
|-
 
|  00:08
 
|  00:08
|* What is '''method overloading.'''
+
|What is '''method overloading.'''
  
 
|-
 
|-
 
|  00:10
 
|  00:10
|* And to overload '''method.'''
+
|And to overload '''method.'''
  
 
|-
 
|-
| 00:13
+
| 00:13
| Here we are using:  
+
| Here we are using:  
* Ubuntu version 11.10  OS
+
'''Ubuntu version 11.10  OS'''
* Java Development kit 1.6
+
'''Java Development kit 1.6'''
* And Eclipse 3.7.0
+
And '''Eclipse 3.7.0'''
  
 
|-
 
|-
| 00:24
+
| 00:24
|   To follow this tutorial, you must know  
+
| To follow this tutorial, you must know  
  
 
|-
 
|-
| 00:26
+
| 00:26
 
|how to create '''methods '''and  
 
|how to create '''methods '''and  
  
 
|-
 
|-
| 00:29
+
| 00:29
 
|to '''overload''' '''constructor''' in java using '''eclipse'''.
 
|to '''overload''' '''constructor''' in java using '''eclipse'''.
  
 
|-
 
|-
| 00:32  
+
| 00:32  
|If not, for relevant tutorials, please visit our website which is as shown.
+
|If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org  
http://www.spoken-tutorial.org  
+
  
 
|-
 
|-
| 00:39
+
| 00:39
 
|What is '''method overloading'''?
 
|What is '''method overloading'''?
 +
 
|-
 
|-
| 00:42
+
|00:42
| Define two or more '''method'''s with same name within a '''class'''.
+
|Define two or more '''methods''' with same name within a '''class'''.
  
 
|-
 
|-
| 00:46
+
|00:46
 
|They must differ in number or types of parameters.
 
|They must differ in number or types of parameters.
  
 
|-
 
|-
| 00:50
+
|00:50
 
|These methods are called '''overloaded methods'''.
 
|These methods are called '''overloaded methods'''.
  
 
|-
 
|-
| 00:54
+
|00:54
 
|The process is called '''method''' '''overloading'''.
 
|The process is called '''method''' '''overloading'''.
  
 
|-
 
|-
| 00:57
+
|00:57
| Let us now see how to '''overload''' a '''method'''.
+
|Let us now see how to '''overload''' a '''method'''.
  
 
|-
 
|-
| 01:00
+
|01:00
 
|In '''eclipse''', I have a class '''Addition.'''
 
|In '''eclipse''', I have a class '''Addition.'''
  
 
|-
 
|-
| 01:06
+
|01:06
 
|Inside the '''class''',  we will  declare two integer variables.
 
|Inside the '''class''',  we will  declare two integer variables.
  
 
|-
 
|-
| 01:10
+
|01:10
|So, type: '''int''' '''a''' is equal to '''10''' and '''int''' '''b''' is equal to '''5.'''
+
|So, type: '''int''' '''a''' equal to '''10''' and '''int''' '''b''' equal to '''5.'''
  
 
|-
 
|-
| 01:19
+
|01:19
| Let us create a '''method''' to add these two integers.
+
|Let us create a '''method''' to add these two integers.
  
 
|-
 
|-
Line 91: Line 88:
 
|-
 
|-
 
|  01:30
 
|  01:30
|Within curly brackets, type: '''System''' dot '''out''' dot '''println'''.
+
|Within curly brackets, type: '''System''' dot '''out''' dot '''println();'''.
  
 
|-
 
|-
Line 98: Line 95:
  
 
|-
 
|-
| 01:44
+
| 01:44
 
|So, this method will give us the sum of two integer variables.
 
|So, this method will give us the sum of two integer variables.
  
 
|-
 
|-
|   01:50
+
| 01:50
| Let us create another '''method''' which takes two parameters.
+
| Let us create another '''method''' which takes two parameters.
 
   
 
   
 
|-
 
|-
| 01:55
+
| 01:55
|So, type: '''void addTwoNumbers'''.
+
|So, type: '''void addTwoNumbers()'''.
 +
 
 
|-
 
|-
| 02:04
+
|02:04
 
|Within parentheses '''int''' '''num1''' comma '''int''' '''num2.'''
 
|Within parentheses '''int''' '''num1''' comma '''int''' '''num2.'''
  
Line 117: Line 115:
  
 
|-
 
|-
| 02:35
+
| 02:35
 
|So, this method will give us the sum of two  values that are passed as arguments to this '''method'''.
 
|So, this method will give us the sum of two  values that are passed as arguments to this '''method'''.
 +
 
|-
 
|-
| 02:44
+
| 02:44
| Let us create an '''object''' of the '''class''' and call the '''method'''s.
+
| Let us create an '''object''' of the '''class''' and call the '''methods'''.
  
 
|-
 
|-
| 02:49
+
|02:49
|So, in  the  '''Main''' method  type: '''Addition''' i.e the class name  '''obj''' is equal to '''new''' '''Addition''' parentheses semicolon.
+
|So, in  the  '''main''' method  type: '''Addition''' i.e the class name  '''obj''' equal to '''new''' '''Addition''' parentheses semicolon.
  
 
|-
 
|-
| 03:13
+
|03:13
|Then '''Obj.add'''   
+
|Then '''Obj.add();'''   
  
 
|-
 
|-
| 03:18
+
|03:18
|And '''Obj.addTwonumbers'''  within parentheses.
+
|And '''Obj.addTwonumbers()'''; within parentheses
  
 
|-
 
|-
| 03:31
+
|03:31
| we  will pass  two arguments.
+
|we  will pass  two arguments.
  
 
|-
 
|-
| 03:33
+
|03:33
 
|Suppose if we pass floating point values.
 
|Suppose if we pass floating point values.
  
 
|-
 
|-
| 03:37
+
|03:37
 
|So, type '''2.5''' comma and an  integer '''3'''.
 
|So, type '''2.5''' comma and an  integer '''3'''.
  
 
|-
 
|-
| 03:45
+
|03:45
| 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'''.
+
|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
+
| 03:57
| So, what we do is, in the method instead of '''int''' we will give '''double'''.
+
| So, what we do is, in the method instead of '''int''' we will give '''double'''.
  
 
|-
 
|-
| 04:06
+
|04:06
 
|So replace '''int''' by '''double''' . '''Save''' the file.  
 
|So replace '''int''' by '''double''' . '''Save''' the file.  
  
 
|-
 
|-
| 04:12
+
|04:12
| we see that the error is resolved.
+
| We see that the error is resolved.
  
 
|-
 
|-
| 04:17
+
|04:17
| we also  know that Java automatically i.e. implicitly coverts '''int''' into '''double'''.
+
| We also  know that Java automatically i.e. implicitly coverts '''int''' into '''double'''.
  
 
|-
 
|-
| 04:24
+
|04:24
 
|So, here we can pass integer arguments as well.
 
|So, here we can pass integer arguments as well.
  
 
|-
 
|-
| 04:28
+
| 04:28
 
| Now  '''Save''' and '''Run''' the program.
 
| Now  '''Save''' and '''Run''' the program.
  
 
|-
 
|-
| 04:32
+
| 04:32
|   In the output, we see the sum of two integer variables  
+
| In the output, we see the sum of two integer variables  
  
 
|-
 
|-
| 04:37
+
|04:37
 
|and the sum of two numeric arguments that we passed.
 
|and the sum of two numeric arguments that we passed.
  
 
|-
 
|-
| 04:43
+
|04:43
| Now we see that both the methods perform same operation.
+
|Now, we see that both the methods perform same operation.
  
 
|-
 
|-
| 04:50
+
|04:50
|The difference is that the first method  has no parameter while the second method has  parameters.
+
|The difference is that the first method  has no parameters while the second method has  parameters.
  
 
|-
 
|-
| 05:00
+
|05:00
 
|So, in such cases java provides us with '''method''' '''overloading'''.  
 
|So, in such cases java provides us with '''method''' '''overloading'''.  
  
Line 200: Line 199:
  
 
|-
 
|-
| 05:09  
+
| 05:09  
|So, replace '''addTwoNumbers''' with '''add'''; also change here.
+
|So, replace '''addTwoNumbers()''' with '''add()''', also change here.
  
 
|-
 
|-
|   05:29
+
| 05:29
| We will define one more method with same operation.
+
| We will define one more method with same operation.
  
 
|-
 
|-
| 05:33
+
|05:33
|So, type: '''void''' '''add'''.
+
|So, type: '''void''' '''add()'''.
  
 
|-
 
|-
| 05:38
+
| 05:38
|And within parentheses '''int n1 '''comma '''int n2''' comma '''int n3.'''
+
| Within parentheses '''int n1 '''comma '''int n2''' comma '''int n3.'''
  
 
|-
 
|-
| 05:51
+
|05:51
|So we have given three parameters.
+
|So, we have given three parameters.
  
 
|-
 
|-
 
| 05:54
 
| 05:54
| Then, within curly brackets '''System''' dot '''out''' dot''' println'''.
+
| Then, within curly brackets '''System''' dot '''out''' dot''' println()'''.
  
 
|-
 
|-
| 06:03
+
| 06:03
 
|Within parentheses '''n1''' plus '''n2''' plus '''n3'''.
 
|Within parentheses '''n1''' plus '''n2''' plus '''n3'''.
  
 
|-
 
|-
| 06:11
+
| 06:11
 
|So, this method will give sum of three numbers.
 
|So, this method will give sum of three numbers.
  
 
|-
 
|-
| 06:17
+
| 06:17
 
| Let us call this '''method'''.
 
| Let us call this '''method'''.
  
 
|-
 
|-
| 06:19
+
| 06:19
|So, type: '''obj''' dot '''add''' '''1''' comma''' 5''' comma '''4'''.
+
|So, type: '''obj''' dot '''add()''' '''1''' comma''' 5''' comma '''4'''.
  
 
|-
 
|-
| 06:35
+
|06:35
| '''Save''' and '''Run'''.   
+
|'''Save''' and '''Run'''.   
  
 
|-
 
|-
| 0639
+
|06:39
| In the output, we see the sum of three number i.e '''10'''.
+
|In the output, we see the sum of three number i.e '''10'''.
  
 
|-
 
|-
| 06:47
+
|06:47
|So, Java compiler overloads the proper method depending on the parameters.
+
|So,Java compiler overloads the proper method depending on the parameters.
  
 
|-
 
|-
| 06:52  
+
| 06:52  
 
|It checks the number and type of parameters passed.
 
|It checks the number and type of parameters passed.
  
 
|-
 
|-
| 06:57
+
|06:57
 
|So, as a programmer we don't have to worry about the '''method''' name.
 
|So, as a programmer we don't have to worry about the '''method''' name.
  
 
|-
 
|-
| 07:01
+
|07:01
 
|Nor even the type or number of arguments we  passed.
 
|Nor even the type or number of arguments we  passed.
  
 
|-
 
|-
|   07:05  
+
|07:05  
| We can create one more '''method''' which appends strings.
+
|We can create one more '''method''' which appends strings.
  
 
|-
 
|-
| 07:11
+
| 07:11
 
|So we will create one more '''overload ''' '''method'''.
 
|So we will create one more '''overload ''' '''method'''.
 +
 
|-
 
|-
| 07:15
+
| 07:15
| Type: '''void add''''' '''String''' '''s1''' comma '''String''' '''s2'''.  
+
| Type: '''void add''''' '''String''' '''s1''' comma '''String''' '''s2'''.  
  
 
|-
 
|-
| 07:29
+
| 07:29
|Within curly brackets '''System''' dot '''out''' dot '''println'''.
+
|Within curly brackets '''System''' dot '''out''' dot '''println()'''.
  
 
|-
 
|-
Line 284: Line 284:
 
|-
 
|-
 
| 07:45
 
| 07:45
| And we will '''call'''  this '''method'''.
+
| Then we will '''call'''  this '''method'''.
  
 
|-
 
|-
| 07:50
+
| 07:50
 
|So, type: '''obj''' dot '''add'''.
 
|So, type: '''obj''' dot '''add'''.
  
Line 295: Line 295:
  
 
|-
 
|-
| 08:07
+
| 08:07
|Now   '''Save''' and '''Run''' the program.
+
|Now '''Save''' and '''Run''' the program.
  
 
|-
 
|-
| 08:12
+
| 08:12
| So, in the output we see '''Hello''' space '''World'''.
+
| So, in the output we see '''Hello''' space '''World'''.
  
 
|-
 
|-
| 08:16
+
| 08:16
 
|So, the '''add''' '''method''' with two string arguments, appends the string.
 
|So, the '''add''' '''method''' with two string arguments, appends the string.
  
 
|-
 
|-
 
| 08:21
 
| 08:21
| Suppose now we declare '''add method''' with '''return''' type.
+
| Suppose now we declare '''add method''' with '''return''' type.
  
 
|-
 
|-
| 08:27
+
| 08:27
 
|So, type: '''int''' '''add''' parentheses  no  parameter and curly brackets.
 
|So, type: '''int''' '''add''' parentheses  no  parameter and curly brackets.
  
 
|-
 
|-
| 08:40
+
| 08:40
|   We get an error.  It states that '''duplicate method add''' in type addition.
+
| We get an error.  It states that '''"Duplicate method add() in type Addition"'''.
  
 
|-
 
|-
| 08:48
+
| 08:48
|This is because we have already declared a  method '''add''' with no parameters.
+
|This is because we have already declared a  method '''add()''' with no parameters.
  
 
|-
 
|-
| 08:54
+
| 08:54
 
|So, remember that to '''overload''' a  method, the parameters must differ.
 
|So, remember that to '''overload''' a  method, the parameters must differ.
  
 
|-
 
|-
| 08:58
+
|08:58
 
|Having different '''return''' types will not '''overload''' the '''method'''.
 
|Having different '''return''' types will not '''overload''' the '''method'''.
  
 
|-
 
|-
| 09:03
+
|09:03
 
|So, remove this method  and '''Save''' the file.
 
|So, remove this method  and '''Save''' the file.
  
 
|-
 
|-
| 09:09
+
| 09:09
 
|This is how '''method overloading''' is done in '''java'''.
 
|This is how '''method overloading''' is done in '''java'''.
  
 
|-
 
|-
| 09:16
+
| 09:16
|   So, in this tutorial we learnt:
+
| So, in this tutorial we learnt:
  
 
|-
 
|-
| 09:18
+
| 09:18
|* About '''method overloading.'''
+
| About '''method overloading.'''
  
 
|-
 
|-
| 09:20
+
| 09:20
|* To''' overload method.'''
+
| To''' overload method.'''
  
 
|-
 
|-
| 09:22
+
| 09:22
|* And advantage of '''method overloading.'''
+
| And advantage of '''method overloading.'''
  
 
|-
 
|-
|   09:25
+
| 09:25
|For self assessment: create a '''method subtract''' that subtracts number,
+
|For self assessment: Create a '''method subtract''' that subtracts number,
  
 
|-
 
|-
 
|09:31   
 
|09:31   
|overload it.  
+
|Overload it.  
  
 
|-
 
|-
 
| 09:33
 
| 09:33
 
| To know more about the Spoken Tutorial project,
 
| To know more about the Spoken Tutorial project,
 +
 
|-
 
|-
| 09:36
+
| 09:36
| watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial]
+
| watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial.
 +
 
 
|-
 
|-
| 09:42
+
| 09:42
 
| It summarizes the Spoken Tutorial project.  
 
| It summarizes the Spoken Tutorial project.  
  
Line 402: Line 404:
 
|-
 
|-
 
|  10:11
 
|  10:11
| More information on this mission is available at  
+
| More information on this mission is available at [http://spoken-tutorial.org/NMEICT-Intro].  
[http://spoken-tutorial.org/NMEICT-Intro].  
+
  
 
|-
 
|-
Line 410: Line 411:
  
 
|-
 
|-
| 10:21
+
| 10:21
|Thanks for joining.
+
|Thanks for joining.This is Prathamesh Salunke, signing off. Jai Hind.  
 
+
|-
+
| 10:22
+
|This is Prathamesh Salunke, signing off. Jai Hind.  
+
  
 
|}
 
|}

Latest revision as of 15:34, 28 March 2017

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