INTRODUCTION:
Heiii friends..In this post I am going to explain about constructors and its types in java..!! I will make sure that you will never ever forget of what is meant by constructors after reading this post..Continue reading to experience it..!!
WHAT IS A CONSTRUCTOR..?
Constructor is something that comes to life when an object creation is done.
Consider the example,
Class Dad ( )
{
public Dad( )
{
//this is the constructor
}
public static void main(String args[] )
{
Dad dadobj= new Dad( );
Dad dadobj= new Dad( );
}
As we all know..
dadobj is the object here but what the new code
public Dad( )
{
}
is doing..?.. Yes.. when the object creation is done ..ie as
soon the reference variable ‘dadobj’ has stored the address of the class
successfully .. and as soon the compiler sees "new " keyword in the right
side.. the compiler assumes the object creation is done successfully and calls
the “SON OF THE CLASS DAD”
Wow.. Son is born for the class DAD and it will be
automatically be born as soon as the compiler sees successful object creation..
So.. SON of the class DAD is born but why the name is same
for the new born constructor still??
Hmm… same question is going on my mind too.. !! :D
All the class is soo
affectionate with their in born so they want the same name to be kept
for their in born too..
They initially had an argument with the compiler ..
initially compiler refused but later it accepted..just to make all the classes
happy :D
So from then on..
The CONSTRUCTOR NAME MUST BE THE SAME AS THE CLASS NAME rule
came into being..
DEFAULT CONSTRUCTOR:
I can give you the same above example for the default
constructor.. because default constructor is the one that will be invoked when
an object creation of the class is done..ie.. when the compiler sees “new”
keyword
If you don’t know what an object creation is.. then please
refer the post objects in java
EXAMPLE FOR DEFAULT CONSTRUCTOR:
Class Dad ( )
{
Public Dad( )
{
System.out.println(“Default constructor is called”);
}
Public static void main(String args[] )
{
Dad dadobj= new Dad( );
Dad dadobj= new Dad( );
}
}
OUTPUT:
Default constructor is called
PARAMETERISED CONSTRUCTOR:
The Constructor with parameters are called “PARAMETERISED
CONSTRUCTOR”
Class Dad ( )
{
Public Dad( )
{
System.out.println(“Default constructor is called”);
}
Public Dad(int x, int y )
{
Int sum = x+ y;
System.out.println(“parameterised constructor is called and
sum is” +sum);
}
Public static void main(String args[] )
{
Dad dadobj= new Dad( );
Dad dadobj= new Dad( );
Dad dadobj2 = new Dad(5,5 );
}
OUTPUT:
Default constructor is called
parameterized constructor is called and sum is 10
EXPLANATION:
Initially object creation is done for the class DAD and then
comes a point to call the constructor..
Now .. since there are many constructors.. compiler will
first check..like.. “ok I got to call an empty constructor..let me go and
check..if there is any empty constructor inside the class” Is what the
compiler plans..
So..it goes inside the class and wow… "I got empty
constructor..it doesn’t have any parameters inside.. so it is the default
constructor and I will print whats got inside it.." is what the compiler doing
Next another object is created for the same class and now
parameters are passed inside the constructor:
COMPILER: Oh.. man I got to search for the constructors
which has got 2 parameters..
Let me search one by one..
Oh.. Its an empty constructor.. I cant execute it..
Let me check with the second constructor..
Arey..wow.. I got a parameterized constructor but I must see
whether the datatype Is correct and number of parameters are correct..
I need 2 parameters and datatype of both is int,int
Wow.. its really a good thing..I just got the same
constructor.. I need not go down and search futher.. :D
Im ssoooo happy !!
So now the compiler is happy and so the clean output
Parameterized constructor is not only with 2 parameter..it
can have any number of parameter .. so every time the compiler will go..grab the
right constructor and will compile..
RULES FOR CONSTRUCTOR:
- Constructor name must be the same as the class name
- Constructor can have any access specifier(PUBLIC,PRIVATE etc) ie. it can have PUBLIC , PRIVATE or PROTECTED keyword in the front..
- It must not have a return type .. ie. return keyword should not be used inside the constructor
- A class can have more than one constructor.. We have seen above.. a class which is having 2 constructor.. one is default and other is parameterized..
CONCLUSION:
Soo… folks.. hope you are aware of what a constructor actually mean.. !! Please hit
like button..drop your valuable feedbacks below ..and help me post more.. !!!
Stay tuned and support for more posts..!!
Suggestions and request the post you wish is also welcomed.
!! Hope you understood the concepts better.. Have a blissful day :)