Java/C2/Parameterized-constructors/English
Title of script: parameterized constructor in java
Author: Prathamesh Salunke
Keywords: video tutorial, constructor, parameterized
|
|
---|---|
Slide 1
Opening slide |
Welcome to the Spoken Tutorial on parameterized constructor in java. |
Slide 2
Learning Objectives |
In this tutorial we will learn
- About a parametrized constructor - To create a parameterized constructor
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Prerequisites |
To follow this tutorial you must know
how to create a default constructor in java using eclipse.
(http://www.spoken-tutorial.org) |
Slide 5
Parameterized Constructor |
A constructor that have parameter is called a parameterized constructor.
|
Switch to eclipse
|
Let us now create a parameterized constructor.
We created this file in the previous tutorial. |
Point to the constructor
Replace “Raman” by null |
Inside the constructor we will give the variables their default value.
And name is equal to null instead of Raman. |
Type:
System.out.println(“I am default constructor”);
|
Then type System dot out dot println I am default constructor.
In java, such constructor is also called default constructor. |
Student(int the_roll_number) | Now we will create another constructor.
So type Student parentheses. Inside parenthesis int the_roll_number comima String the_name. |
Highlight Student | Here what we have done is declared a constructor with parameters.
The name of the constructor Student that is the class name. |
Highlight (the_roll_number, String the_name) | Inside paranthesis we have given two parameters to the constructor.
We can give any number of parameters. |
Type:
System.out.println(“I am a parameterized constructor”); |
Now inside curly brackets type
System dot out dot println I am a parameterized constructor |
Type:
roll_number=the_roll_number; name=the_name; |
Then roll_number is equal to the_roll_number.
And name is equal to the_name.
|
Highlight the parameters | Information that we want to pass to the constructor is received by these variables. |
Save and Run | Save and Run the program |
Point to the output | We see the output on the console. |
Highlight output | The default constructor is called first.
It initializes the variables to their default value. Then the parameterizerd constructor is called. It initializes the variables to the values that are passed as the argument. That is 11 and Raju. |
Hightlight arguments | Let us see how the parametrized constructor works.
When we call the parameterized constructor, we pass two variables to it. These are called arguments. |
Highlight the parameters | 11 is copied into the parameter the_roll_number.
Raju is copied into the parameter the_name. |
Highlight
roll_number=the_roll_number; name=the_name; |
The value of the_roll_number is assigned to roll_number.
And the value of the_name is assigned to name. Hence we see the output 11 and Raju. |
Delete “Raju” | Let us see the common errors, when we call a parameterised constructor.
So remove Raju. |
Point to the error | We see an error. It states that “The constructor Student(int) is undefined.”
So the number of arguments must match the number of parameters. Here we can retype Raju to resolve the error. Alternatively, we can define another constructor with a single parameter. |
Student(int r_no)
{ System.out.println(“I am a constructor with a single parameter”); roll_number=r_no; } |
Let us create another constructor.
Type Student within paranthesis int r number. Inside curly brackets, System dot out dot println I am a constructor with a single parameter. Then roll_number is equalto r number |
Save the file | Save the file.
We see the error is resolved as we define the constructor. |
Run | Run the program. |
Point to the output | On the console we see the roll number is assigned the value 11.
While name is null since the constructor takes only one argument. |
Student stu3=new Student(11,”Raju”);
|
Let us now call back our constructor with two parameters.
So type Student stu3 is equal to new Student. Within paranthesis 11 comma Raju. Stu3 dot studentDetails |
Student(“11”,”Raju”); | Suppose here we pass 11 as String, so add double quotes.
We get an error. It states that “The constructor Student String String is undefined.” So even the datatype of the argument must match with that of parameter. |
Remove “”.
Save the file. |
Remove the quotes and Save the file.
Now we do not see an error. |
Run the program | Run the program. |
Point to the output | We see three constructors.
The default constructor. Constructor with one parameter. Constructor with two parameters. |
Slide: Why constructor? | The variables in a class must be initialized each time an instance is created.
|
Slide 6
Summary |
So in this tutorial, we have learnt
To create parameterized constructor. Functionality of parameterized constructor. The advantage of using constructor.
|
Slide 7
Assignment |
For self assessment, create a class Employee.
Create constructors with different number of parameters.
|
Slide 8
About Slide |
To know more about the Spoken Tutorial Project
|
Slide 9
About Slide
|
The Spoken Tutorial Project Team
|
Slide 10
Acknowledgment |
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
We have come to the end of this tutorial.
Thanks for joining. This is Prathamesh Salunke signing off. Jai Hind. |