Java/C2/Parameterized-constructors/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: parameterized constructor in java

Author: Prathamesh Salunke

Keywords: video tutorial, constructor, parameterized


Visual Cue
Narration
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
  • Ubuntu version 11.10
  • Java Development Environment jdk 1.6
  • Eclipse 3.7


Slide 4

Prerequisites

To follow this tutorial you must know

how to create a default constructor in java using eclipse.


If not, for relevant tutorials please visit our website which is as shown,

(http://www.spoken-tutorial.org)

Slide 5

Parameterized Constructor

A constructor that have parameter is called a parameterized constructor.


It can have one or more than one parameter.

Switch to eclipse


Point to the file.

Let us now create a parameterized constructor.


So in the eclipse, I have the Student.java file.

We created this file in the previous tutorial.

Point to the constructor


Replace 10 by 0

Replace “Raman” by null

Inside the constructor we will give the variables their default value.


So roll_number is equal to zero instead of ten.

And name is equal to null instead of Raman.

Type:

System.out.println(“I am default constructor”);


Point to the constructor.

Then type System dot out dot println I am default constructor.


So we have created a constructor with no parameters.

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.


So we have created a parameterized constructor

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.


Suppose we pass a single argument to the 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”);


stu3.studentDetail();

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.


It can be tedious to initialize all the variables.


Java allows objects to initialize themselves when they are created.


This is performed through the use of a constructor.

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
  • If you do not have good bandwidth, you can download and watch it


Slide 9

About Slide


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact@spoken-tutorial.org


Slide 10

Acknowledgment

Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


We have come to the end of this tutorial.

Thanks for joining.

This is Prathamesh Salunke signing off.

Jai Hind.

Contributors and Content Editors

Chandrika