Difference between revisions of "Java/C2/Constructor-overloading/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(7 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|- | |- | ||
− | | | + | | 00:03 |
| Welcome to the Spoken Tutorial on '''constructor overloading''' in''' java'''. | | Welcome to the Spoken Tutorial on '''constructor overloading''' in''' java'''. | ||
|- | |- | ||
− | | | + | | 00:08 |
− | | | + | | In this tutorial, we will learn |
− | + | ||
|- | |- | ||
− | | | + | | 00:10 |
− | | | + | | what is '''constructor overloading''' |
− | + | ||
|- | |- | ||
− | | | + | | 00:13 |
− | | | + | | and to '''overload''' '''constructor'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 00:16 |
− | | | + | | Here we are using: |
− | + | '''Ubuntu version 11.10 OS''' | |
− | + | '''Java Development kit 1.6''' | |
− | + | '''Eclipse 3.7.0''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 00:27 |
− | | | + | | To follow this tutorial, you must know |
− | + | ||
|- | |- | ||
− | | | + | | 00:30 |
− | | how to create '''constructors''' in | + | | how to create '''constructors''' in java using '''eclipse'''. |
− | + | ||
− | + | ||
|- | |- | ||
| 00:34 | | 00:34 | ||
− | | 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 | ||
− | |||
|- | |- | ||
− | | | + | | 00:40 |
− | | What is constructor overloading? | + | | What is '''constructor overloading'''? |
+ | |||
|- | |- | ||
| 00:43 | | 00:43 | ||
− | | Define multiple '''constructors''' for a class. | + | | Define '''multiple''' '''constructors''' for a '''class'''. |
+ | |||
|- | |- | ||
| 00:46 | | 00:46 | ||
Line 65: | Line 56: | ||
| 00:50 | | 00:50 | ||
| Let us now see how to overload '''constructor'''. | | Let us now see how to overload '''constructor'''. | ||
− | |||
|- | |- | ||
| 00:54 | | 00:54 | ||
− | | In '''eclipse''', I have a class '''Student''' with two variables and a method. | + | | In '''eclipse''', I have a '''class''' '''Student''' with two variables and a '''method'''. |
|- | |- | ||
| 01:03 | | 01:03 | ||
| Let us first create a '''parameterized constructor.''' | | Let us first create a '''parameterized constructor.''' | ||
− | |||
|- | |- | ||
| 01:07 | | 01:07 | ||
− | | So | + | | So, type: '''Student '''within parentheses''' int number '''comma''' String the_name.''' |
− | + | ||
|- | |- | ||
| 01:26 | | 01:26 | ||
− | | | + | |Within curly brackets, type: '''roll_number''' is equal to '''number.''' |
− | + | ||
|- | |- | ||
| 01:38 | | 01:38 | ||
− | | And | + | | And ''' name''' is equal to '''the_name''' |
− | + | ||
− | + | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | So we have a '''constructor''' with two parameters. | + | | So, we have a '''constructor''' with two '''parameters'''. |
|- | |- | ||
| 01:51 | | 01:51 | ||
− | | | + | | Let us call this '''constructor'''. |
− | + | ||
|- | |- | ||
| 01:53 | | 01:53 | ||
− | | So in '''main method''' type | + | | So, in '''main method''' type: '''new Student''' parentheses semicolon. |
|- | |- | ||
| 02:03 | | 02:03 | ||
| We see an error, it states that '''constructor''' '''Student''' is undefined. | | We see an error, it states that '''constructor''' '''Student''' is undefined. | ||
− | |||
|- | |- | ||
| 02:10 | | 02:10 | ||
| This is simply because we have defined a '''constructor''' with two parameters. | | This is simply because we have defined a '''constructor''' with two parameters. | ||
− | |||
|- | |- | ||
Line 121: | Line 103: | ||
|- | |- | ||
| 02:22 | | 02:22 | ||
− | | So we need to | + | | So, we need to pass '''arguments'''. |
− | + | ||
|- | |- | ||
| 02:25 | | 02:25 | ||
− | | So within parentheses type | + | | So, within parentheses type: 22 comma in double quotes '''Ram'''. |
− | + | ||
|- | |- | ||
Line 135: | Line 115: | ||
|- | |- | ||
| 02:36 | | 02:36 | ||
− | | | + | |Let us call the '''method'''. |
− | + | ||
|- | |- | ||
| 02:38 | | 02:38 | ||
− | | So before '''new '''type''' Student s | + | | So, before '''new ''', type: '''Student s''' is equal to '''new student'''. |
+ | |||
|- | |- | ||
| 02:45 | | 02:45 | ||
− | | Now using the object '''s''' we will call the method ''' studentDetail()''' | + | | Now, using the object '''s''' we will call the method ''' studentDetail()'''. |
|- | |- | ||
Line 151: | Line 131: | ||
|- | |- | ||
| 02:58 | | 02:58 | ||
− | | We see the output '''22''' and '''Ram.''' | + | | We see the output: '''22''' and '''Ram.''' |
|- | |- | ||
|03:03 | |03:03 | ||
− | | Now | + | | Now let us define a '''constructor''' with no parameter. |
− | + | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | | So type | + | | So, type: '''Student''' parentheses. |
− | + | ||
|- | |- | ||
| 03:12 | | 03:12 | ||
− | | | + | |Within curly brackets '''roll_number''' is equal to''' 0.''' |
− | + | ||
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | And | + | | And ''' name''' is equal to in double quotes hyphen that is no name. |
|- | |- | ||
| 03:30 | | 03:30 | ||
− | | So now we can call the '''constructor''' with no parameters. | + | | So, now we can call the '''constructor''' with no parameters. |
− | + | ||
|- | |- | ||
| 03:35 | | 03:35 | ||
− | | So type | + | | So, type: '''Student''' '''s1''' is equal to '''new''' '''Student''' parentheses semicolon. |
|- | |- | ||
| 03:47 | | 03:47 | ||
− | | This time we see no error, since we have | + | | This time we see no error, since we have defined a '''constructor''' without parameter. |
+ | |||
|- | |- | ||
− | | | + | | 03:55 |
− | | | + | | Then ''' s1 '''dot''' studentDetail();'''. |
|- | |- | ||
Line 194: | Line 171: | ||
|- | |- | ||
| 04:04 | | 04:04 | ||
− | | So in the output we see '''zero''' and '''dash''' when the | + | | So, in the output we see '''zero''' and '''dash''' when the default '''constructor''' is called. |
− | + | ||
|- | |- | ||
| 04:11 | | 04:11 | ||
| This is '''constructor''' '''overloading'''. | | This is '''constructor''' '''overloading'''. | ||
− | |||
|- | |- | ||
| 04:13 | | 04:13 | ||
− | | We have two ''' | + | | We have two '''constructors''' with different parameters. |
− | + | ||
|- | |- | ||
| 04:17 | | 04:17 | ||
− | | Both the ''' | + | | Both the '''constructors''' obviously have same name. |
− | + | ||
|- | |- | ||
| 04:20 | | 04:20 | ||
− | | So depending on the type and number of | + | | So, depending on the type and number of parameters, the '''constructor''' is called. |
|- | |- | ||
| 04:26 | | 04:26 | ||
− | | | + | | Let us see the advantage of '''constructor''' '''overloading'''. |
− | + | ||
|- | |- | ||
| 04:30 | | 04:30 | ||
− | | Suppose now call a constructor with two parameters. | + | | Suppose, now we call a constructor with two parameters. |
− | + | ||
|- | |- | ||
| 04:35 | | 04:35 | ||
− | | So type | + | | So, type: '''Student s3= new Student();''' |
− | + | ||
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | Now within parentheses, suppose | + | | Now within parentheses, suppose I gave the '''name''' argument first and then the '''roll_number.''' |
|- | |- | ||
| 04:58 | | 04:58 | ||
− | | | + | | Let's see what happens.So, in double quotes '''Raju''' comma '''45'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 05:08 | | 05:08 | ||
− | | We see an error which states that | + | | We see an error which states that "The constructor Student with the parameter String comma int is undefined." |
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | So let us define the constructor. | + | | So, let us first define the constructor. |
|- | |- | ||
| 05:22 | | 05:22 | ||
− | | So type | + | | So, type: '''Student''' within parentheses '''String''' '''the_name''' comma '''int''' '''r_no'''. |
|- | |- | ||
| 05:42 | | 05:42 | ||
− | |So over here first parameter is '''string'' and the second parameter is '''int''' | + | |So, over here first parameter is '''string''' and the second parameter is '''int'''. |
+ | |||
|- | |- | ||
| 05:52 | | 05:52 | ||
− | |Then | + | |Then, within curly bracket, '''roll_number''' is equal to '''r_no;''' |
|- | |- | ||
| 06:05 | | 06:05 | ||
− | | | + | | and '''name''' is equal to '''the_name;''' |
|- | |- | ||
Line 275: | Line 242: | ||
|- | |- | ||
− | | | + | | 06:22 |
− | | Let us call the method. | + | |Let us call the '''method'''. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | So | + | | So, '''s3''' dot '''studentDetail();''' |
|- | |- | ||
| 06:29 | | 06:29 | ||
− | | | + | | '''Save''' the program and '''Run'''. |
|- | |- | ||
| 06:35 | | 06:35 | ||
− | |So we see the output'''45''' and '''Raju''' | + | |So, we see the output '''45''' and '''Raju'''. |
|- | |- | ||
| 06:40 | | 06:40 | ||
− | | So here we see that when we call the '''constructor''' | + | | So, here we see that when we call the '''constructor''' |
|- | |- | ||
| 06:43 | | 06:43 | ||
− | | | + | | we do not have to worry about the parameters that we are passing. |
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | This is simply because we have | + | | This is simply because we have defined multiple '''constructors''' with different parameters. |
|- | |- | ||
| 06:54 | | 06:54 | ||
− | | So the proper '''constructor''' is '''overloaded.''' | + | | So, the proper '''constructor''' is '''overloaded.''' |
|- | |- | ||
− | | | + | | 06:57 |
| We can therefore now define a '''constructor''' which takes only one parameter. | | We can therefore now define a '''constructor''' which takes only one parameter. | ||
Line 316: | Line 283: | ||
|- | |- | ||
| 07:05 | | 07:05 | ||
− | | So type | + | | So, type: '''Student''' within parentheses '''int num''', |
|- | |- | ||
| 07:16 | | 07:16 | ||
− | | | + | | within curly brackets '''roll_number '''is equal to '''num'''. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | | | + | | And '''name''' is equal to "no name". |
|- | |- | ||
| 07:33 | | 07:33 | ||
− | | Now let us call this '''constructor''' | + | | Now, let us call this '''constructor'''. |
+ | |||
|- | |- | ||
| 07:43 | | 07:43 | ||
− | | So type | + | | So, type: '''Student''' '''s4''' is equal to '''new''' '''Student;'''. This time we will pass a single argument. So, let us pass '''61'''. |
|- | |- | ||
| 08:04 | | 08:04 | ||
− | | Then | + | | Then '''s4''' dot ''' studentDetail(); ''' |
|- | |- | ||
| 08:10 | | 08:10 | ||
− | | '''Save''' and '''Run''' the program | + | | '''Save''' and '''Run''' the program. |
|- | |- | ||
− | | | + | | 08:14 |
− | | So in the output we see the '''roll | + | | So, in the output we see the '''roll number''' as '''61''' and '''name''' as '''no name.''' |
|- | |- | ||
| 08:21 | | 08:21 | ||
− | | As we can see, the proper overloaded '''constructor''' is called when new is executed. | + | | As we can see, the proper overloaded '''constructor''' is called when '''new''' is executed. |
|- | |- | ||
| 08:27 | | 08:27 | ||
− | | Based upon the parameters specified the proper '''constructor''' is overloaded. | + | | Based upon the parameters specified, the proper '''constructor''' is overloaded. |
|- | |- | ||
Line 358: | Line 326: | ||
|- | |- | ||
− | | | + | |08:40 |
− | | | + | | So, in this tutorial, we have learnt: |
|- | |- | ||
| 08:42 | | 08:42 | ||
− | | About the '''constructor overloading | + | |About the '''constructor overloading''' |
|- | |- | ||
| 08:45 | | 08:45 | ||
− | | | + | |To overload '''constructor''' and the use of constructor overloading |
|- | |- | ||
|08:50 | |08:50 | ||
− | | | + | | For self assessment, create multiple constructors for class '''Employee''' and '''overload''' the '''constructor.''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 08:58 | | 08:58 | ||
− | | | + | | To know more about the Spoken Tutorial project, |
+ | |||
|- | |- | ||
| 09:00 | | 09:00 | ||
− | | | + | |watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial]. |
+ | |||
|- | |- | ||
| 09:06 | | 09:06 | ||
− | | | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 09:09 | | 09:09 | ||
− | | | + | | If you do not have good bandwidth, you can download and watch it. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 09:12 |
− | | | + | | The Spoken Tutorial project team: |
|- | |- | ||
| 09:15 | | 09:15 | ||
− | | | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
| 09:17 | | 09:17 | ||
− | | | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
| 09:20 | | 09:20 | ||
− | | | + | | For more details, please write to contact@spoken-tutorial.org |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 09:26 |
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
|- | |- | ||
| 09:30 | | 09:30 | ||
− | | | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 09:35 | | 09:35 | ||
− | | | + | | More information on this mission is available at [http://spoken-tutorial.org/NMEICT-Intro]. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 09:43 |
− | | | + | | This brings us to the end of the tutorial. |
|- | |- | ||
| 09:46 | | 09:46 | ||
− | | Thanks for joining. | + | | Thanks for joining.This is Prathamesh Salunke, signing off. Jai Hind. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|} | |} |
Latest revision as of 16:09, 9 March 2017
Time | Narration |
00:03 | Welcome to the Spoken Tutorial on constructor overloading in java. |
00:08 | In this tutorial, we will learn |
00:10 | what is constructor overloading |
00:13 | and to overload constructor. |
00:16 | Here we are using:
Ubuntu version 11.10 OS Java Development kit 1.6 Eclipse 3.7.0 |
00:27 | To follow this tutorial, you must know |
00:30 | how to create constructors in java using eclipse. |
00:34 | If not, for relevant tutorials, please visit our website which is as shown. |
00:40 | What is constructor overloading? |
00:43 | Define multiple constructors for a class. |
00:46 | They must differ in number or types of parameters. |
00:50 | Let us now see how to overload constructor. |
00:54 | In eclipse, I have a class Student with two variables and a method. |
01:03 | Let us first create a parameterized constructor. |
01:07 | So, type: Student within parentheses int number comma String the_name. |
01:26 | Within curly brackets, type: roll_number is equal to number. |
01:38 | And name is equal to the_name |
01:46 | So, we have a constructor with two parameters. |
01:51 | Let us call this constructor. |
01:53 | So, in main method type: new Student parentheses semicolon. |
02:03 | We see an error, it states that constructor Student is undefined. |
02:10 | This is simply because we have defined a constructor with two parameters. |
02:16 | And we are calling a constructor without parameters. |
02:22 | So, we need to pass arguments. |
02:25 | So, within parentheses type: 22 comma in double quotes Ram. |
02:33 | We see that the error is resolved. |
02:36 | Let us call the method. |
02:38 | So, before new , type: Student s is equal to new student. |
02:45 | Now, using the object s we will call the method studentDetail(). |
02:53 | Save the program and Run. |
02:58 | We see the output: 22 and Ram. |
03:03 | Now let us define a constructor with no parameter. |
03:07 | So, type: Student parentheses. |
03:12 | Within curly brackets roll_number is equal to 0. |
03:21 | And name is equal to in double quotes hyphen that is no name. |
03:30 | So, now we can call the constructor with no parameters. |
03:35 | So, type: Student s1 is equal to new Student parentheses semicolon. |
03:47 | This time we see no error, since we have defined a constructor without parameter. |
03:55 | Then s1 dot studentDetail();. |
04:01 | Save and Run the program. |
04:04 | So, in the output we see zero and dash when the default constructor is called. |
04:11 | This is constructor overloading. |
04:13 | We have two constructors with different parameters. |
04:17 | Both the constructors obviously have same name. |
04:20 | So, depending on the type and number of parameters, the constructor is called. |
04:26 | Let us see the advantage of constructor overloading. |
04:30 | Suppose, now we call a constructor with two parameters. |
04:35 | So, type: Student s3= new Student(); |
04:51 | Now within parentheses, suppose I gave the name argument first and then the roll_number. |
04:58 | Let's see what happens.So, in double quotes Raju comma 45. |
05:08 | We see an error which states that "The constructor Student with the parameter String comma int is undefined." |
05:18 | So, let us first define the constructor. |
05:22 | So, type: Student within parentheses String the_name comma int r_no. |
05:42 | So, over here first parameter is string and the second parameter is int. |
05:52 | Then, within curly bracket, roll_number is equal to r_no; |
06:05 | and name is equal to the_name; |
06:15 | Save the program. |
06:18 | Now we see that the error is resolved. |
06:22 | Let us call the method. |
06:24 | So, s3 dot studentDetail(); |
06:29 | Save the program and Run. |
06:35 | So, we see the output 45 and Raju. |
06:40 | So, here we see that when we call the constructor |
06:43 | we do not have to worry about the parameters that we are passing. |
06:47 | This is simply because we have defined multiple constructors with different parameters. |
06:54 | So, the proper constructor is overloaded. |
06:57 | We can therefore now define a constructor which takes only one parameter. |
07:02 | That is roll number. |
07:05 | So, type: Student within parentheses int num, |
07:16 | within curly brackets roll_number is equal to num. |
07:25 | And name is equal to "no name". |
07:33 | Now, let us call this constructor. |
07:43 | So, type: Student s4 is equal to new Student;. This time we will pass a single argument. So, let us pass 61. |
08:04 | Then s4 dot studentDetail(); |
08:10 | Save and Run the program. |
08:14 | So, in the output we see the roll number as 61 and name as no name. |
08:21 | As we can see, the proper overloaded constructor is called when new is executed. |
08:27 | Based upon the parameters specified, the proper constructor is overloaded. |
08:33 | This is how constructor overloading is done. |
08:40 | So, in this tutorial, we have learnt: |
08:42 | About the constructor overloading |
08:45 | To overload constructor and the use of constructor overloading |
08:50 | For self assessment, create multiple constructors for class Employee and overload the constructor. |
08:58 | To know more about the Spoken Tutorial project, |
09:00 | watch the video available at [1]. |
09:06 | It summarizes the Spoken Tutorial project. |
09:09 | If you do not have good bandwidth, you can download and watch it. |
09:12 | The Spoken Tutorial project team: |
09:15 | Conducts workshops using spoken tutorials. |
09:17 | Gives certificates to those who pass an online test. |
09:20 | For more details, please write to contact@spoken-tutorial.org |
09:26 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
09:30 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
09:35 | More information on this mission is available at [2]. |
09:43 | This brings us to the end of the tutorial. |
09:46 | Thanks for joining.This is Prathamesh Salunke, signing off. Jai Hind. |