Java/C2/Default-constructor/English-timed
From Script | Spoken-Tutorial
Revision as of 16:52, 31 March 2015 by Sandhya.np14 (Talk | contribs)
Time | Narration |
00:02 | Welcome to the Spoken Tutorial on default constructor in java. |
00:07 | In this tutorial, we will learn: |
00:10 | About the default constructor. |
00:12 | And to create a constructor. |
00:15 | Here we are using
|
00:26 | To follow this tutorial, you must know |
00:29 | how to create a class and the object of the class in java using eclipse. |
00:34 | If not, for relevant tutorials please visit our website which is as shown. |
00:42 | Constructor is used to initialize the instance variables. |
00:46 | It is called at the creation of new object. |
00:50 | Let us now see how constructor is defined in java. |
00:55 | So in the eclipse, I have already created a java file, Student.java. |
01:02 | In the Student class we will declare two variables. |
01:07 | So type int roll_number semi-colon and String name semicolon. |
01:20 | Now let us create a method. |
01:22 | So, type: void studentDetail(). |
01:33 | Within curly brackets, type: System dot out dot println roll_number |
01:50 | Then System dot out dot println name. |
02:03 | Now in Main method we will call this method. |
02:08 | So, let us create an object and call the method. |
02:14 | So type Student object name stu equal to new Student. |
02:28 | Then stu dot method name i.e studentDetail. |
02:41 | Save and Run the program. |
02:46 | We see the output zero and null. |
02:49 | So, the int variable roll_number has been initialized to its default value zero. |
02:56 | And the String name has been initialized to its default value null. |
03:02 | If we do not define a constructor then a default constructor is created. |
03:08 | Default constructor has no parameters. |
03:11 | It initializes the instance variables to their default value. |
03:16 | Now let us define a constructor. |
03:18 | So, type: Student parenthesis and curly brackets. |
03:30 | Remember the Constructor has the same name as the class name to which it belongs. |
03:38 | Constructors are also similar to methods but there are some important differences. |
03:44 | Save and Run the program. |
03:48 | We see the same output. |
03:51 | This is because the constructor that we defined is same as having no constructor. |
03:58 | But here no default constructor is created because we have defined a constructor. |
04:06 | Now, let's give values to our variables. |
04:11 | So, inside the constructor type: roll_number equal to ten semicolon. |
04:25 | And name equal to within double quotes Raman. |
04:35 | Now Save and Run the program. |
04:43 | We see in the output that the roll_number value is ten and name is Raman. |
04:50 | So, the constructor initializes the instance field. |
04:55 | Now, let us see some difference between method and a constructor. |
05:01 | Constructor does not have a return type. |
05:05 | whereas method has a return type. |
05:10 | Constructor is called using the new operator. |
05:16 | whereas the method is called using the dot operator. |
05:21 | So, these were some differences between constructor and method. |
05:29 | So, in this tutorial, we have learnt: |
05:32 | * About the default constructor. |
05:34 | * To define a constructor. |
05:36 | * And Differences between method and constructor. |
05:41 | For self assessment, |
05:42 | create a class Employee with variables and a method to display variables. |
05:47 | And create a constructor for the class Employee. |
05:52 | To know more about the Spoken Tutorial Project |
05:54 | Watch the video available at [1] |
06:00 | It summarizes the Spoken Tutorial project. |
06:03 | If you do not have good bandwidth, you can download and watch it. |
06:06 | The Spoken Tutorial project team: |
06:08 | Conducts workshops using spoken tutorials. |
06:11 | Gives certificates to those who pass an online test. |
06:14 | For more details, please write to contact@spoken-tutorial.org |
06:20 | Spoken Tutorial project is a part of the Talk to a Teacher project. |
06:24 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
06:29 | More information on this mission is available at: |
06:38 | This brings us to the end of the tutorial. |
06:40 | Thanks for joining. |
06:42 | This is Prathamesh Salunke, signing off. Jai Hind. |