Difference between revisions of "Java/C2/Constructor-overloading/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 19: | Line 19: | ||
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | and to overload '''constructor'''. | + | | and to '''overload''' '''constructor'''. |
|- | |- | ||
| 00:16 | | 00:16 | ||
− | | 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 | ||
Line 66: | Line 66: | ||
|- | |- | ||
| 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.''' | + | |Within curly brackets, type: '''roll_number''' is equal to '''number.''' |
|- | |- | ||
Line 78: | Line 78: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | So we have a '''constructor''' with two parameters. | + | | So, we have a '''constructor''' with two '''parameters'''. |
|- | |- | ||
Line 86: | Line 86: | ||
|- | |- | ||
| 01:53 | | 01:53 | ||
− | | So in '''main method''' type '''new Student''' parentheses | + | | So, in '''main method''' type: '''new Student''' parentheses semicolon. |
|- | |- | ||
Line 102: | Line 102: | ||
|- | |- | ||
| 02:22 | | 02:22 | ||
− | | So we need to pass '''arguments'''. | + | | So, we need to pass '''arguments'''. |
|- | |- | ||
| 02:25 | | 02:25 | ||
− | | So within parentheses type '''22''' comma in double quotes '''Ram'''. | + | | So, within parentheses type: '''22''' comma in double quotes '''Ram'''. |
|- | |- | ||
Line 114: | Line 114: | ||
|- | |- | ||
| 02:36 | | 02:36 | ||
− | |Let us call the method. | + | |Let us call the '''method'''. |
|- | |- | ||
| 02:38 | | 02:38 | ||
− | | So before '''new ''' type '''Student s''' is equal to '''new student'''. | + | | 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 138: | Line 138: | ||
|- | |- | ||
| 03:07 | | 03:07 | ||
− | | So | + | | So, type: '''Student''' parentheses. |
|- | |- | ||
Line 146: | Line 146: | ||
|- | |- | ||
| 03:21 | | 03:21 | ||
− | | And ''' name is equal to in double quotes hyphen | + | | 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 | | 03:55 | ||
− | | Then | + | | Then ''' s1 '''dot''' studentDetail'''. |
|- | |- | ||
Line 181: | Line 181: | ||
|- | |- | ||
| 04:17 | | 04:17 | ||
− | | Both the '''constructor''' obviously have same name. | + | | Both the '''constructor'''s 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. | + | | Let us see the advantage of '''constructor''' '''overloading'''. |
|- | |- | ||
Line 197: | Line 197: | ||
|- | |- | ||
| 04:35 | | 04:35 | ||
− | | So type '''Student s3= new Student();''' | + | | 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. |
|- | |- | ||
| 04:59 | | 04:59 | ||
− | | So in | + | | So, in double quotes '''Raju''' comma '''45'''. |
|- | |- | ||
Line 217: | Line 217: | ||
|- | |- | ||
| 05:18 | | 05:18 | ||
− | | So let us define the constructor. | + | | So, let us 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 | ||
Line 244: | Line 244: | ||
|- | |- | ||
| 06:22 | | 06:22 | ||
− | | Let us call the method. | + | | Let us call the '''method'''. |
|- | |- | ||
| 06:24 | | 06:24 | ||
− | | So '''s3 | + | | So, '''s3''' dot '''studentDetail.''' |
|- | |- | ||
| 06:29 | | 06:29 | ||
− | | '''Save''' the program and '''Run''' | + | | '''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 '''constructor''' with different parameters. |
|- | |- | ||
| 06:54 | | 06:54 | ||
− | | So the proper '''constructor''' is '''overloaded.''' | + | | So, the proper '''constructor''' is '''overloaded.''' |
|- | |- | ||
Line 284: | Line 284: | ||
|- | |- | ||
| 07:05 | | 07:05 | ||
− | | So type '''Student''' within parentheses '''int num''' | + | | So, type: '''Student''' within parentheses '''int num''' |
|- | |- | ||
| 07:16 | | 07:16 | ||
− | | within curly brackets '''roll_number '''is | + | | within curly brackets '''roll_number '''is equal to '''num'''. |
|- | |- | ||
| 07:25 | | 07:25 | ||
− | | And '''name''' is equal to | + | | And '''name''' is equal to '''no''' '''name'''. |
|- | |- | ||
Line 300: | Line 300: | ||
|- | |- | ||
| 07:43 | | 07:43 | ||
− | | So type '''Student''' '''s4 | + | | So, type: '''Student''' '''s4''' is equalto '''new''' '''Student'''. This time we will pass a single argument. So let us pass '''61'''. |
|- | |- | ||
Line 308: | Line 308: | ||
|- | |- | ||
| 08:10 | | 08:10 | ||
− | | '''Save''' and '''Run''' the program | + | | '''Save''' and '''Run''' the program. |
|- | |- | ||
Line 316: | Line 316: | ||
|- | |- | ||
| 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 328: | Line 328: | ||
|- | |- | ||
| 08:40 | | 08:40 | ||
− | | So in this tutorial, we have learnt | + | | 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 | + | | For self assessment, create multiple constructors for class '''Employee''' and '''overload''' the '''constructor.''' |
|- | |- | ||
| 08:58 | | 08:58 | ||
− | | To know more about the Spoken Tutorial | + | | 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 | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 09:09 | | 09:09 | ||
− | | If you do not have good bandwidth, you can download and watch it | + | | If you do not have good bandwidth, you can download and watch it. |
|- | |- | ||
| 09:12 | | 09:12 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
| 09:15 | | 09:15 | ||
− | | Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
| 09:17 | | 09:17 | ||
− | | Gives certificates to those who pass an online test | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
Line 375: | Line 375: | ||
|- | |- | ||
| 09:26 | | 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 | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 09:35 | | 09:35 | ||
− | | More information on this | + | | More information on this mission is available at |
− | + | [http://spoken-tutorial.org/NMEICT-Intro] | |
− | + | ||
|- | |- | ||
| 09:43 | | 09:43 | ||
− | | This brings us to the end of the tutorial | + | | This brings us to the end of the tutorial. |
|- | |- | ||
Line 397: | Line 396: | ||
|- | |- | ||
| 09:47 | | 09:47 | ||
− | | This is Prathamesh Salunke signing off. Jai Hind. | + | | This is Prathamesh Salunke, signing off. Jai Hind. |
|} | |} |
Revision as of 18:20, 1 April 2015
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:
|
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 constructor with different parameter. |
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 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. |
04:59 | 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 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 constructor 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 equalto 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. |
09:47 | This is Prathamesh Salunke, signing off. Jai Hind. |