What is constructor?
Constructor is a special type of method that always holds the class name and it is mainly used to initialize the object. It construct the values for the object .So it is known as constructor.
Constructor is of two types:
[i] Zero argument constructor(default constructor)
[ii] Parameterized constructor
Zero Argument Constructor: When we don’t pass any argument in constructor. That is known as zero argument constructor.If user defined constructor is not there then compiler will generate zero argument constructor known as default constructor.
Parameterized constructor: When we pass the argument in constructor known as parameterized constructor. Advance java training in bangalore In it we can pass any no. Of argument.
Properties of Constructor :
It is same as the class name.
It does not having any return type or return statement.
It has access modifiers.
It is neither static nor nonstatic.
What is Constructor Chaining:
Calling a constructor from another constructor , is known as constructor chaining.If we want to access the properties of any constructor then we have to use constructor chaining.
We know that constructor is neither static nor non static. So we can’t inherit the constructor.But using constructor chaining we can acquire the properties of another constructor.
Note: Constructor is a special type of method that always holds the class name.java/j2ee classes bangalore It is not a method but it consist of the same behaviour as a method.
How can we achieve the constructor chaining ?
There is two ways to achieve the constructor chaining:
 super() : super() (call to super) is used to call the super class constructor. That mean sub class is able to access the properties of constructor which are present in super class. For accessing the super class constructor properties inheritance is must.
If user is not defined the super() then compiler generates as the first statement in the class.But if user defined constructor is there compiler never generate java course in bangalore the super().
 this(): this()(call to this) is used to call the same class constructor. That mean we can achieve the properties of constructor which are present in the same class. For using this() constructor overloading is necessary. Without constructor overloading we can’t get the properties of same class.
Note: super() and this() should be the first statement. Because of that both can’t be present in the same class.
What is need of constructor chaining ?
Access the properties: Constructor chaining is used to access the properties of constructor in the same class or in another class.
Code optimization :Using constructor chaining java training center bangalore we achieve code optimization ,means lines of code get reduced.
Maintenance easy : Using constructor chaining maintenance easy because if we keep the common functionality in super class constructor.So we need to change only in super class constructor.
Memory will get reduced : If same code will repeat at multiple times then more memory will used. to avoid this we use constructor chaining, we access the same code.Then less memory used.
Difference between super() and this()
1) super() is used to call super class constructor, best core java training in bangalore this() is used to call same class constructor.
2) For super() inheritance is necessary because for achieving the super class constructor we use the concept of sub and super class. For this() constructor overloading is necessary.
3) If userdefined super() is not there compiler generate super().But this() is not generate by the compiler, we have to explicitly define the this().
This is short description about the constructor & constructor chaining. To get the more knowledge on java and getting the free 100% job support join Infocampus Software Training Institute for making the career in java
Please Rate this Article
5 out of 54 out of 53 out of 52 out of 51 out of 5
Not yet Rated