Java Essentials - Constructors of a class
Show More Show Less View Video Transcript
0:00
We are discussing Constructed of a Class. Constructed of a class is nothing but
0:05
ordinary a member function, but this constructor will be invoked by default automatically
0:11
when an object will be defined under the class. And that is a very special feature with
0:16
the constructor methods. So what is constructor? Constructors are similar to a function or
0:23
method defined within the class as a member method, which is invoked automatically when an object
0:29
will be created under the class. So let us go for some other features of the constructor
0:35
Constructors have the foreign properties. So the constructor has no return type
0:40
specification. When we define one constructor, we need not to mention the return type
0:46
What is the return type of the constructor? So constructor does not require any return type
0:51
specification. The name of the constructor must be same as that of the class name
0:56
So depending upon the class name, the constructor name will be same as the up the class name maintaining the upper case and lower case letters as usual
1:03
constructors are used to initialize the class members we can have two types
1:08
of constructors one is a parameter as constructed where you are passing some input arguments to the constructor method Another one is the non constructor and this constructor will have no input parameter
1:21
These constructors are there to initialize the member variables. So that is the main purpose
1:26
of a constructor. So when you define one object under the class, then construct a method will get
1:31
invoked automatically and the constructor will initialize the member variables. if we do not define any constructor the compiler will automatically generate a
1:42
default constructor that means if we do not define any constructor in our
1:47
program the constructor will be defined by the compiler that is a non-parameterized
1:51
default constructor a constructor should be having the public scope but sometimes
1:58
we put for some special purposes so it might be keeping a constructor under the
2:02
private scope say in one design pattern that is a singleton design pattern
2:06
we define our constructor in the private scope but that is a very special case we always define
2:11
our constructor under the public scope constructors cannot be inherited so that's that's why we're
2:17
having multiple different properties of this constructor to have a better idea let us go for one
2:22
practical demonstration where it will be showing that in our java code how we can define constructors in this demonstration we shall discuss Java constructors We know that constructors are nothing but functions whose name will be same as that of the class name
2:38
Here we have defined one class, the name of the class is account, and we have defined three instance variable, name account number and balance
2:47
So this is known as the constructor here. We are not passing any parameter
2:51
So that's why it is called parameter as constructor, whose name will be same
2:54
as that of the class name where the return type specification is not there it is under the public
3:00
section and here we are initializing the instance variable name and account number with the null
3:07
string and balance with the 0.0 this is another constructor whose name will be same as that of the
3:13
class name having no return type specification under the public scope and it is parameter as here
3:19
we are passing three parameters that is a string n string id and double b
3:25
So now what will happen this name and this account number and this balance they will get updated they will get the initialized with this input parameters and then we're having the set details where we are passing this name and the respective ID
3:39
the name and ID will get instantiated will get updated will get initialized using the set details This is my deposit money Where the money whatever will be passing as input argument that will get added with the balance
3:53
and the balance will be the sum of the previous balance plus the money which is being passed as the input parameter
4:00
the value of the money will get added and the new balance will be there and the balance value will get increased in that case
4:06
So now this is the get details. actually the get details means here we're having the display function actually so it is
4:12
printing the name printing the account number and printing this balance with
4:17
this prompt text here so it is they are getting concordinated with the
4:21
respective texts and strings and then the respective values are going to get
4:26
displayed so here the name of the class is constructed so that's why you can find
4:31
that we are having this constructor. Java and under the main function we have
4:35
defined two objects under the class account and these two objects one has got instantiated with the non parameterist
4:43
constructor another has got instantiated with a parameter as constructor so it has
4:47
gone for the respective name it has gone for the ID and it is gone for the
4:52
balance here but in case of non parameter as constructed we didn't pass
4:56
anything so the name and the respective balance and
#Java (Programming Language)
#Java (Programming Language)

