Java/C2/Constructor-overloading/English
Title of script: Constructor overloading
Author: Prathamesh Salunke
Keywords: video tutorial, constructor, overloading
|
|
---|---|
Slide 1
Opening slide |
Welcome to the Spoken Tutorial on constructor overloading in java. |
Slide 2
Learning Objectives |
In this tutorial we will learn
- what is constructor overloading - To overload constructor
|
Slide 3
System Requirements |
Here we are using
|
Slide 4
Prerequisites |
To follow this tutorial you must know
how to create constructors in java using eclipse.
(http://www.spoken-tutorial.org) |
Slide 5
Constructor Overloading |
Define multiple constructors which differ in number or types of parameters. |
Switch to Eclipse | Let us now see how to overload constructor.
In eclipse, I have a class Student with two variables and a method. |
Type:
Student(int number, String the_name) { roll_number=number; name=the_name; } |
Let us first create a parameterized constructor.
So type, Student within parentheses int number comma String the_name. Within curly brackets, roll_number is equal to number. And name is equalto the_name
|
Type:
new Student(); |
Now let us call the constructor.
So type new Student parentheses semi colon |
Point to the Error. | We see an error that the constructor Student is not defined.
This is simply because we have defined a constructor with parameters. And we are calling a constructor without parameters. |
Type:
Student(22,”Ram”); |
So let us pass arguments.
Type 22 comma in double quotes Ram. We see that the error is resolved. |
Type:
Student s = new Student(22,”Ram”); s.studentDetail(); |
Let us call the method.
So before new type Student s is equal to. Then s dot studentDetail(); |
Save and Run | Save and Run the program. |
Point to the Output | We see the output 22 and Ram. |
Type:
Student() { roll_number=0; name=”-”; } |
Now let us define a default constructor.
So type, Student parentheses. Within curly brackets roll_number is equal to 0. And name is equal to in double quotes hypen. |
Type:
Student s2 = new Student();
|
So now we can call the constructor with no parameters.
So type Student s1 is equal to new Student parentheses. |
Type:
s2.studentDetail(); |
Then s1 dot studentDetail. |
Save and Run | Save and Run the program. |
Point to the Output | We see the output zero and dash when default constructor is called.
This is constructor overloading. We have two constructor with difference in parameter. Both the constructor obviously have same name. So depending on the type and number of parameter, the constructor is called. |
Type:
Student s3=new Student(“Raju”,45); |
Let us see the advantage of constructor overloading.
Suppose when we call a constructor with two parameters. So type Student s3= new Student(); And suppose if we pass the name argument first and then the roll number. That is in double quotes Raju comma 45 |
Point to the Error | We see an error that constructor String comma int is not defined. |
Type:
Student(String the_name, int r_no) { roll_number=r_no; name=the_name; } |
So let us define the constructor.
So type Student within parentheses String the_name comma int r_no Within curly bracket, roll_number is equal to r_no. And name is equal to the_name. |
Save the file | Save the file.
We see that the error is resolved. |
Type:
s3.studentDetail(); |
Now call the method.
So type, s3 dot studentDetail. |
Save and Run | Save and Run the file. |
Point to the Output | We see 45 and Raju on the console.
So here we see that when we call the constructor. We do not have to worry about the parameters we are passing. This is because we have define multiple constructor with different parameters. So the proper constructor is overloaded. |
Type:
Student(int num) { roll_number=num; } |
We can therefore define constructor which takes only one parameter.
That is roll number. So type Student within parentheses int num. Then within curly brackets roll_number is equalto num. And name is equal to in double quotes no name. |
Student s4 = new Student(61);
s4.studentDetail(); |
So now when we call the constructor and give one integer argument.
So type Student s4 is equalto new Student within parentheses 61 Then s4 dot studentDetail |
Save and Run the program | Save and Run the program |
Point to the Output | So in the output we see roll number as 61 and name as no name.
As we can see, the proper overloaded constructor is called when new is executed. Based upon the parameters specified the proper constructor is overloaded. This is how constructor overloading is done. |
Slide 6
Summary |
So in this tutorial, we have learnt
What is constructor overloading. How to overload constructor.
|
Slide 7
Assignment |
For self assessment, create multiple constructors for class Employee.
Overload the constructor.
|
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. |