Lecture 10: Constructors in Java - Default and Parameterized

IIT Roorkee July 201
61 followers
Jan 9, 2025
[Music]
[Music]
so welcome to the last lecture of
classes and objects in the last lecture
here we had seen separately how to use
default Constructor and parameters
Constructors so now so can we use both
in a single program right so that we are
going to see in this case study or the
problem right so let us consider
employee management
system all right so that is the class so
that has employee details such as you
have ID name age and salary all right so
these are all the member data kind of
all right so now the main idea is we
have to use both default and
parameterized
Constructors right so in the same
program so when you are going to create
the employee objects so we are going to
use default and parameterized
Constructors so how we are splitting
this the default Constructor should
contains or
initialize the employee with the values
ID is zero name unknown age should be 18
and salary is zero okay and then the
parameters Constructor should
initialize so you should have a employee
object let us say another object let us
say
E2 with certain values by the user so
given by the user the specific values
given by the user for ID name age and
salary also the program should in
include the display employee details so
we have to display the employee details
also we have to do the update in the
salary right for example if some
employee is getting the salary of 30,000
it should be 40,000 and similarly 3
lakhs it should be 4 lakh something like
that that should be update and that
update we have to display so the main
idea here is how we can use both default
and parameterized Constructors so we'll
see the program all right so we'll see
the C++ program so I have class
employee so here I have four member data
as suggested in the problem so we have
ID whose data type is integer name whose
data type is string age integer and
salary is double okay so this is your
member data and here you
have a constraint Constructor right so
this is a default Constructor you don't
have any parameters so therefore you
call this as a default Constructor so in
the default Constructor your ID is zero
name is unknown age is equal to 18
salary is 0.0 right you have one see out
statement the see out statement is
printing ID okay default Constructor
call the employee with ID so this is an
undercode ID will be printed right in
fact ID is zero zero should be printed
so whenever you are creating an object
all right without any parameters so you
call this is a default Constructor so it
will be automatically invoked and then
the sequence of steps will
be right executed and you have one C out
statement so that means in the console
you will get this particular output so
now parameter Constructor so here you go
the parameters Constructor so you can
see both default Constructor and
parameters Constructors so they are
having same name as class also you can
find the usual function we call this as
a special member function because you
can't find any return type so any member
function you can find a return type it
may be a wide it may be integer it may
be double Etc so whereas in this case
you don't have any return type right so
that is why it is also called the
special member function so now the par
petress Constructor you have employee ID
string of employee name employee age and
employee
salary all right so under this you have
right so whatever you are passing as a
parameter passing as a value in the main
all right so that will be initialized to
ID name age and salary so again you are
displaying the parameters Constructor
call for employee with ID so whatever be
the ID so that particular ID will be
printed so now in the question it was
asked we have to do the display details
right this is a special member function
and here you go you are printing
employee ID which is
ID right name you have name age you have
age all right and salary you have the
salary right so this we are printing in
the display details that means I want to
print all the ID name age and salary of
the employees and then your update
salary right so I'm passing the update
salary as a parameter all right I'm
passing as a value all right so it will
be the new salary under double all right
so the new salary will be assigned to
salary that means there is a
modification in the salary there is a
updation in the salary so in the see out
statement
you can see salary updated for employee
with ID right so you have ID and salary
will be displayed and you can look here
the class is ending over here okay so we
have completed the class so now when we
go to the main program so you have
created the first object emp1 under
employee class you have emp1 right so
whenever you are creating this object we
know that right so whenever you are
creating this object so your uh default
Constructor will be automatically called
right employee want the default
Constructor so in the default
Constructor so what is there so we had
seen so the default Constructor you have
ID zero all right name is unknown age is
18 salary is 0.0 so initialize and then
it is see out statement and then you
have one see out statement so there you
have default Constructor call for
employee with ID all right so the ID
will be
printed what is the id id is zero so you
will get the output default Constructor
call for employee with id id right so
that will be printed right so this is
what the next line line number 51 you
have emp1 do display details so the
display details here you go all will be
printed right the ID zero name is
unknown a age is 18 and salary is 0.0 so
this will be printed so we have done up
to line number 51 and line number 54
right emp1 is invoking update salary
right and then you are passing 30,000
all right so when you are updating this
salary so now new salary will be
assigned to salary so now that means
when you are calling emp1 that means
emp1 is invoking display details that is
a change all right so there is a change
so what is the change you can have right
so this is the one zero unknown 18
0.0 and then since you have this update
all right so you will have salary
updated for employee with id0 to 30,000
right so that will be printed for
employee one all right I hope it is
clear and then see out endl that means
it it will go to the next line so now
you are creating a second object right
and then you will have the
parameterz right you are passing the
parameters so therefore the parameters
Constructor will be invoked all right so
parameters Constructor when invoked
you're pausing 99 ashmin 38 and 400,000
all right so that
means when you go here your employ ID so
ID will be 99 name will be Ashwin age 38
and salary will be 400,000 so the id id
is 99 will be printed over here right so
when emp2 is invoking display details
all these information will be printed in
line number 60 and uh there is a salary
update all right that 4 lakhs will be
now 5 lakhs 400,000 will be now 500,000
so in that case when emp2 is invoking
update salary
500,000 so you can see that all right so
here the new salary is 500,000 so that
will become the salary in line number 42
that will be assigned to salary so
salary updated for employee with id99 to
500,000 will be
printed right so this is what exactly
happening and then you have return zero
then you can see the list of output all
right so initially
your employee ID is zero right so
whenever you are creating an object
right whenever you are creating an
object so in the default Constructor so
you have this display
message all right in the default
Constructor all right so in the default
Constructor you have this message what
is the message the default Constructor
called for employee with ID zero right
so that will be printed so it is what
you are getting as a first output and
then employee details employee ID is
zero name is unknown age is 18 and
salary is zero and similarly employee ID
right for there is a salary update yes
there is a salary update so the salary
update function when you are calling
right when employee one is invoking so
you are getting this output salary
updated for employee with ID 0 to 30,000
right employee ID zero so
now he or she is going to get 30,000
that is the meaning and uh rest of them
are similar ID name age and if you look
salary now it is becoming 30,000 so this
is the default Constructor and in the
parameterized Constructor all right so
whenever it is calling when you're
creating an object
emp2 all right under the employee ID 99
all right so employee ID is 99 name is
Ashwin
age is 38 and salary is 400,000 right so
initially and there is a salary update
salary update has gone from 400,000 to
500,000 so when emp2 is
invoking right the
details all right employee details or
updated details so salary is updating
from 400,000 to 500,000 so that will be
displayed for id99 to 500,000 and rest
ID name age is same and salary there is
a change now the display is
500,000 all right I hope you have
understood this particular program so
here we can see right in this problem so
they are asking us to use both
Constructors default Constructor as well
as the parameter Constructor and I hope
it is very clear now so similarly in
Java we will see the concept of default
Constructor right so let us consider in
this case there are two classes right in
this case there is a class bike one all
right and then you have in fact this is
a driver class right so you have void
main so therefore you call this as a
driver class so you have only one class
and in this class itself you have a void
main so you call this as a driver class
in Java all right so the class B one and
this carries same name so we know that
from C++ so we know that this is a
Constructor and no parameter therefore
you call it as a default Constructor so
here system.out.print it is equalent to
C out bike is created right so this is
what it's going to display in the case
of construct when you are creating an
object so now here you are creating an
object so slightly different from our
usual C++ so it's a everything is
dynamic in Java so here you have an
object small B under the class B one and
dynamically you are creating a memory
for bike one so new is the operator for
dynamically allocating a memory the new
operator and bike one right it's a
Constructor correct so
B so this B will
have will be in some address right is
object object B will be in some address
so now we are printing out
system.out.print and B right so let us
see what is the output we are going to
get first when you're creating an object
bike is created will be printed and the
second one right so we'll see this is
the output we are going to get so the
second output so we have run in uh uh
shell all right the Linux so Java C is
for compilation bye one. Java is the
name of the file and the second one is
running the program Java Blank Space
bike one all right so you can see the
output bike is created will be printed
so that means whenever you are creating
an object default Constructor will be
invoked and bike is created will be
printed all right and next one you call
this as the identity Ash
map all right you call address in
C++ right you call this as address in
C++ in Java you call this as identity
Ash map so when you're trying to print B
the object in fact the location is
equivalent to this all right the
location the location
in Java you call it as
identity Ash
map all right so that identity Ash map
will be printed so the address
location other words it's called address
location so now what will happen if I
create exactly like your C++ bike B all
right bike B I have not put any right
print statement so when you are creating
an object so what is happening so we'll
see right I try to compile and run the
code all right so you can see unlike in
C++ first of all it is not calling the
default
construct program is running right I can
able to compile and run the code
all right so whereas if you look I am
not getting any
output correct so the object is being
created but it is not invoking the
default Constructor so line number eight
so that is the reason so it should be
the dynamic in nature so if you look at
line number eight so this is the right
way to do in Java so bike one B that
means B is the object B one is a class
that is equal to the new operator and
byon exactly like the dynamic array that
you do it right it's a dynamic object
all right so we have done right or you
might have done int star a I put star in
in
C++ equal to
new in of
10 Dynamic array of length 10 all right
this is in
C++ the same code in Java in you have
the array notation a
equal to new in of 10 so when you create
an array you use this so similarly here
new right new and then bike one right
the same inte a data type so here we are
writing int with the size so here you
are writing bike one it's like a
Constructor exactly you're putting the
Constructor syntax or notation all right
but the problem is when you're not
dynamically allocated the memory so even
the default Constructor will not be
called so now we will see right the
program will run all right there is it's
not giving any error so I can I can able
to compile Java C stands for compile bye
one. Java is a name of the file and for
executing or running the code we use
Java by one without extension so you can
try in Linux or you might have already
done in the case of the basic
programming right procedural oriented
programming so now what I will do I'll
just remove the common statement right
so here line number nine I made a common
statement I able to right run the code
but with common statement what is
happening right so without common
statement what is happening so now if I
try to run this code so I will get an
error right so B is not being
initialized right the error message that
I am getting B is not initialized right
so that means you are not able to
dynamically create a memory for B so
where it is being stored the compiler
does not know right so when I try to
compile itself you are getting the
compilation
error all right where it is being stored
and then you trying to write the storage
location right you're trying to write
the identity Ash map or you're going to
find out the identity Ash map right so
therefore if you look I am getting an
error right so line number eight what we
have correctly did so this is required
right so dynamically we have to allocate
the memory so maybe we'll look at one
program with the combination of both
default and parameter Constructor in
Java all right so let us consider class
perimeter right so let us consider class
perimeter so there you are having two
member data one is length and another
one is breadth right so here you have
the default
Constructor so under this default
Constructor your length is zero and
breadth is
zero right your length is zero and
breadth is zero in the default
Constructor you are initializing length
equal to zero and breadth is equal to
zero all right so now we will see the
parameters Constructor parameter
Constructor you have two parameters
right so I have two arguments one is int
X and int y so x i Am assigning to
length and Y I am assigning to breadth
all right so now I have one method so
the member function in C++ in Java you
call it as a method so calore perimeter
we are calculating the perimeter so the
perimeter is nothing but 2 into length
plus breadth right the perimeter is
nothing but 2 into length plus
bread Okay so
so it is printing so whatever you are
calculating will be assigning to Perry
and
system.out.print L the perimeter of the
rectangle is right so you are printing
the perimeter and line number 25 your
class is getting over right your class
is getting ended over here so now if I
go to the main all right so class const
example is a driver class right so you
have two classes here so one class is
perimeter right since you don't have a
main over here this is not a driver
class and here you go so const example
is the driver
class so in the driver class you have a
main
program right so I have two objects
here right one is perimeter P1 P1 is an
object we are using the new operator and
perimeter means it's a default
Constructor so now you are understanding
right so the bike we have done the same
thing bike one b equal to new by one
right it's a default Constructor it is
calling and here line number 32 you can
see perimeter P2 P2 is an object so that
is equal to new perimeter of 25 comma
100 you are passing 25 comma 100 so when
I creating P1 one right what is
happening so when I creating P1 so when
I'm creating P1 the default Constructor
will be called your length is zero and
breadth is zero that's it right your
length is zero and breadth is zero I
hope you understand length is zero and
breath is zero and when you are creating
an object P2 you are pausing 25 and 100
all right when you are calling P2 to you
are pausing 25 and 100 so what is
happening we'll see when you're pausing
25 and 100 so this is your parameterized
Constructor X will take the value 25 and
Y will take the value 100 so now that
will be assigned to length and breadth
for object P2 right so this is what
exactly happening all right so now we'll
see what are the what are the further
methods it is calling so now P1 is
invoking the method calore
perimeter right so P1 is invoking calore
perimeter so when P1 is invoking this so
here you go all right so you have a
local variable Perry we know for the P1
length is zero and breadth is zero right
so this is what happening in the default
Constructor so the default Constructor
when I go back so you can see over here
your length is zero and breadth is zero
correct so now when it is calculating 2
into 0 so the result should be zero in
the case of first
object right your X is zero and Y is
zero right so the perimeter should be
zero and in the second case you're
passing 25 and 100 right when it is
calling calculating the perimeter
function right calcal underscore
perimeter Method All right what is
happening you're passing
x25 all right length becomes
25 all right so length becomes 25 and
breadth becomes 100 so we know that 100
+ 25 125 125 into 2 so I have to get the
output 250 for second
case right I have to get 250 for the
second case so first output is zero
second output should be 250 so this is
what the output we have to get so when I
run the code all right so Java C so
const example. Java is the name of the
program right name of the file const
example. Java so Java C is a compilation
and next one next line we are running
the code execution so Java const example
without extension so here you can see
the output the first output is the
perimeter of the rectangle is
zero all right even you can modify the
perimeter of the first rectangle or the
first object right so whatever you're
writing this is going to be printed
right so the perimeter of the rectangle
in fact yeah so here you can modify so
here right line number 23 so we can
modify accordingly but the case is you
have the member function or method so
you have written the perimeter of the
rectangle so that is what it is printing
maybe what we can do in the Constructor
itself we will write see out some
statement right so we are trying to find
out the perimeter of the rectangle
correct something like that otherwise in
both the cases we are getting the same
output so you may ask the question right
so this we can control in the
Constructor you make one see out
statement I mean to say system. out.
print LM statement so the perimeter of
the rectangle is zero and the perimeter
of the rectangle you are getting
250 right so in this
chapter we had seen extensively all
right you have in fact initially I
started with objects and classes all
right so you know how to define a class
and how to create the objects and now
you can also know how to create the
dynamic objects with the help of right
in Java right so you know the difference
between the static object and dynamic
object so in particular right you can
use a static object in C++ which is not
a problem we still call the default
Constructor whereas we are finding some
problem in Java when you are creating
only the static object the program is
running but when you're trying to find
out what is a location or identity Ash
map it is giving a error so in the case
of java so what we have to do we have to
create the dynamic objects so once you
are creating the dynamic object the
syntax should be like a line number 31
and 32
right so you are creating two objects P1
and P2 so P1 you can see the right hand
side new that is the operator and then
you have the Constructor so that
Constructor is nothing but the default
Constructor all right so this is nothing
but the default Constructor and the
second case it is a parameterized
Constructor so
P2 your classes perimeter with the help
of new operator so you have default
Constructor so you can try the similar
code in C++ also all right so the
dynamic nature it will work because in
the case of array I gave an example in
Star a equal to new int of 10 right
you're using the star operator all right
the D reference
operator correct so in a similar way
when you have
the Constructor like a default
Constructor and parameterized
Constructor when there is no arguments
you call this as a default Constructor
so this is a default Constructor and if
you're passing certain parameters right
if it is having or you're passing
certain values or in the Constructor if
it is having some arguments you call
this as a parameterized Constructor so
you know whenever you are creating an
object so which one should be called and
as I also said the constructors are
nothing but the special member function
or the special method it looks exactly
like a method or it is is looking
exactly like a function but it does not
have any return type all right so this
is what even in Java also we had seen
all right so line number 13 if you look
it does not have any return type and
they have the same name as of class so
therefore it is called the constructors
so in both the programs we had seen
usage of default and parameters
Constructors in the same program right
so we had seen different example the
default parameterized separately and in
C++ we had solved one case study the
combination of default and parameters
and similarly in the case of java we had
seen the default Constructor and also
one example of using both default and
paramet Constructor so in this chapter
we extensively
studied this class objects and the
constructors so with this I'm concluding
thank you
[Music]
