1 00:00:02 --> 00:00:07 Welcome to the Spoken Tutorial on parameterized constructor in java. 2 00:00:08 --> 00:00:09 In this tutorial we will learn: 3 00:00:10 --> 00:00:12 * About a parameterized constructor. 4 00:00:13 --> 00:00:16 * And to create a parameterized constructor. 5 00:00:17 --> 00:00:28 Here we are using: Ubuntu version 11.10 OS Java Development kit 1.6 And Eclipse 3.7.0 6 00:00:29 --> 00:00:31 To follow this tutorial, you must know 7 00:00:32 --> 00:00:36 how to create a default constructor in java using eclipse. 8 00:00:37 --> 00:00:43 If not, for relevant tutorials, please visit our website which is as shown. http://www.spoken-tutorial.org 9 00:00:44 --> 00:00:47 What is a parameterized constructor? 10 00:00:48 --> 00:00:54 A constructor that has parameters is called a parameterized constructor. 11 00:00:55 --> 00:00:58 It can have one or more than one parameters. 12 00:00:59 --> 00:01:02 Let us now create a parameterized constructor. 13 00:01:03 --> 00:01:08 So in the eclipse, I have Student.java file. 14 00:01:09 --> 00:01:14 We created this file in the previous tutorial. 15 00:01:15 --> 00:01:20 Now, inside the constructor we will give the variables their default value. 16 00:01:21 --> 00:01:26 So roll_number is equal to zero instead of ten. 17 00:01:27 --> 00:01:32 And name is equal to null instead of Raman. 18 00:01:33 --> 00:01:54 Then type System dot out dot println "I am a default constructor". 19 00:01:55 --> 00:01:59 So, we have created a constructor with no parameters. 20 00:02:00 --> 00:02:06 In java, such constructor is also called a default constructor. 21 00:02:07 --> 00:02:10 Now we will create another constructor. 22 00:02:11 --> 00:02:16 So, type: Student parentheses. 23 00:02:17 --> 00:02:35 Inside parenthesis int the_roll_number comma String the_name. 24 00:02:36 --> 00:02:42 So, what we have done is declared a constructor with parameters. 25 00:02:43 --> 00:02:48 The name of the constructor is Student that is the class name. 26 00:02:49 --> 00:02:56 Inside the parentheses, we have given two parameters to the constructor. 27 00:02:57 --> 00:03:01 We can give any number of parameters to the constructor. 28 00:03:02 --> 00:03:04 Now, inside curly brackets type: 29 00:03:05 --> 00:03:28 System dot out dot println "I am a parameterized constructor". 30 00:03:29 --> 00:03:42 Then, roll_number is equal to the_roll_number. 31 00:03:43 --> 00:03:52 And name is equal to the_name. 32 00:03:53 --> 00:03:57 So, we have created a constructor with parameters. 33 00:03:58 --> 00:04:01 Now let's call this constructor. 34 00:04:02 --> 00:04:27 So, in the main method type: student stu2 equal to new student within parentheses 11 comma in double quotes Raju. 35 00:04:28 --> 00:04:30 Let's call the studentDetail method. 36 00:04:31 --> 00:04:37 So, type: stu2.studentDetail. 37 00:04:38 --> 00:04:43 Save and Run the program. 38 00:04:44 --> 00:04:47 We see the output on the console. 39 00:04:48 --> 00:04:51 The default constructor is called first. 40 00:04:52 --> 00:04:55 It initializes the variables to their default values. 41 00:04:56 --> 00:04:59 Then the parameterized constructor is called. 42 00:05:00 --> 00:05:04 It initializes the variables to the values that are passed as the arguments. 43 00:05:05 --> 00:05:07 That is 11 and Raju. 44 00:05:08 --> 00:05:11 Let us see how the parameterized constructor works. 45 00:05:12 --> 00:05:17 When we call the parameterized constructor, we pass two values to it. 46 00:05:18 --> 00:05:21 These are called arguments. 47 00:05:22 --> 00:05:30 The value 11 is copied to the parameter the_roll_number. 48 00:05:31 --> 00:05:40 And the value Raju is copied to the parameter the_name. 49 00:05:41 --> 00:05:49 Then the value of the_roll_number is assigned to roll_number. 50 00:05:50 --> 00:05:54 And the value of the_name is assigned to name. 51 00:05:55 --> 00:05:59 Hence, in the output we see 11 and Raju. 52 00:06:00 --> 00:06:06 Let us see the common errors when we call a parameterized constructor. 53 00:06:07 --> 00:06:10 Suppose, we pass a single argument to the constructor. 54 00:06:11 --> 00:06:14 So remove Raju. 55 00:06:15 --> 00:06:23 We get an error. It states that “The constructor Student with parameter (int) is undefined.” 56 00:06:24 --> 00:06:29 So, the number of arguments must match the number of parameters. 57 00:06:30 --> 00:06:35 Here we can retype Raju and resolve the error. 58 00:06:36 --> 00:06:41 Alternatively, what we can do is define another constructor with a single parameter. 59 00:06:42 --> 00:06:44 Let us do that. 60 00:06:45 --> 00:07:00 So, Student within parentheses int r_ number. 61 00:07:01 --> 00:07:12 Inside curly brackets, type: System dot out dot println 62 00:07:13 --> 00:07:28 "I am a constructor with a single parameter". 63 00:07:29 --> 00:07:47 Then roll_number is equal to r_ number. 64 00:07:48 --> 00:07:50 Save the file. 65 00:07:51 --> 00:07:57 We see that the error is resolved when we define the constructor. 66 00:07:58 --> 00:08:01 Let's Run the program. 67 00:08:02 --> 00:08:07 On the console we see that the roll number is assigned the value 11. 68 00:08:08 --> 00:08:17 While name is null since the constructor takes only one argument. 69 00:08:18 --> 00:08:22 Let us now call back our constructor with two parameters. 70 00:08:23 --> 00:08:39 So, type Student stu3 is equal to new Student. 71 00:08:40 --> 00:08:45 11 comma Raju. 72 00:08:46 --> 00:08:57 Then Stu3 dot studentDetail. 73 00:08:58 --> 00:09:07 Suppose here we pass 11 as String, so add double quotes. 74 00:09:08 --> 00:09:09 We get an error. 75 00:09:10 --> 00:09:16 It states that “The constructor Student String comma String is undefined.” 76 00:09:17 --> 00:09:24 So even the data type of the argument must match with that of the parameter. 77 00:09:25 --> 00:09:31 So now remove the quotes and Save the file. 78 00:09:32 --> 00:09:34 Now we do not see an error. 79 00:09:35 --> 00:09:37 So Run the program. 80 00:09:38 --> 00:09:41 In the output we see three constructors. 81 00:09:42 --> 00:09:44 The first one is the default constructor. 82 00:09:45 --> 00:09:49 The second one is the Constructor with one parameter. 83 00:09:50 --> 00:09:55 And this third one is the Constructor with two parameters. 84 00:09:56 --> 00:10:04 This is how we create parameterized constructor in java. 85 00:10:05 --> 00:10:06 Why constructor? 86 00:10:07 --> 00:10:12 The variables in a class must be initialized each time an instance is created. 87 00:10:13 --> 00:10:17 It can be tedious to initialize all the variables. 88 00:10:18 --> 00:10:24 So, java allows objects to initialize themselves when they are created. 89 00:10:25 --> 00:10:29 This is performed through the use of a constructor. 90 00:10:30 --> 00:10:32 So in this tutorial, we have learnt: 91 00:10:33 --> 00:10:35 * To create parameterized constructor. 92 00:10:36 --> 00:10:38 * Functionality of parameterized constructor. 93 00:10:39 --> 00:10:43 * And the advantage of using constructor. 94 00:10:44 --> 00:10:47 For self assessment, create a class Employee. 95 00:10:48 --> 00:10:52 Create constructors with different number of parameters. 96 00:10:53 --> 00:10:55 To know more about the Spoken Tutorial project, 97 00:10:56 --> 00:11:01 watch the video available at [1]. 98 00:11:02 --> 00:11:05 It summarizes the Spoken Tutorial project. 99 00:11:06 --> 00:11:09 If you do not have good bandwidth, you can download and watch it. 100 00:11:10 --> 00:11:11 The Spoken Tutorial project team: 101 00:11:12 --> 00:11:13 Conducts workshops using spoken tutorials. 102 00:11:14 --> 00:11:17 Gives certificates to those who pass an online test. 103 00:11:18 --> 00:11:23 For more details, please write to contact@spoken-tutorial.org 104 00:11:24 --> 00:11:27 Spoken Tutorial Project is a part of the Talk to a Teacher project. 105 00:11:28 --> 00:11:33 It is supported by the National mission on Education through ICT, MHRD, Government of India. 106 00:11:34 --> 00:11:42 More information on this mission is available at: [2]. 107 00:11:43 --> 00:11:45 This brings us to the end of the tutorial. 108 00:11:46 --> 00:11:46 Thanks for joining. 109 00:11:47 --> 00:11:52 This is Prathamesh Salunke, signing off. Jai Hind.