Java/C2/Method-overloading/English-timed

From Script | Spoken-Tutorial
Revision as of 14:45, 9 July 2014 by Gaurav (Talk | contribs)

Jump to: navigation, search


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 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 is equalto 10 and int b is equalto 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 parameter.
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 argument to this methods.
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 is equalto new Addition parentheses semicolon.
03:13 ThenObj.add
03:18 AndObj.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 which states that , the method addTwoNumbers int comma int of the class addition is not applicable for the argument double comma int.
03:57 So what we do is in the method instead of int we will give double.
04:06 So replaceintby 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 an integer arguments as well.
04:28 Now Save and Run the program
04:32 In the output we see the sum of two integers variables,
04:37 And the sum of two numeric arguments that we passed.
04:43 Now we see that both the method performs same operation.
04:50 The difference is that the first method has no parameter. While the second method has parameters.
05:00 So in such case 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 opearation.
05:33 So type void add.
05:38 AndWithin parentheses int n1 comma int n2 comma int n3.
05:51 So we have given three parameters.
05:54 One 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
0639 In the output we see the sum of three number i.e10
06:47 So Java compiler overloads the proper method depending on the parameters.
06:52 It checks the number and type of parameter passed
06:57 So as a programmer we dont have worry about the method name.
07:01 Nor even the type or number of argument we passed.
07:05 We can create one more method which append 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 And 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 method the parameters must differ.
08:5 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 subtract that 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 Give 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
http://spoken-tutorial.org/NMEICT-Intro 


10:19 We have come to the end of this tutorial.
10:21 Thanks for joining.
10:22 This is Prathamesh Salunke signing off. Jai Hind.

Contributors and Content Editors

Devisenan, Gaurav, Jyotisolanki, PoojaMoolya, Pratik kamble, Sandhya.np14, Sneha