Java/C2/Constructor-overloading/English-timed
From Script | Spoken-Tutorial
| 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,
(http://www.spoken-tutorial.org) |
| 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 name method type new Student parentheses semi colon |
| 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 recall 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 hypen 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 define 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 and default constructor is called.
|
| 04:11 | This is constructor overloading.
|
| 04:13 | We have two constructor with different parameter.
|
| 04:17 | Both the constructor obviously have same name.
|
| 04:20 | So depending on the type and number of parameter, 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 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 output45 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 define 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 equalto 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 an 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 Employeeand Overload the constructor.
|
| 08:58 | To know more about the Spoken Tutorial Project |
| 09:00 | Watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial |
| 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 |
| 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. |