Constructor in Java is a method with no rerun type and name is same as class name, which has really it;s own importance.
In Java, we have two types of constructor:-
Default Constructor :-
A Default Constructor is a constructor which does not take any argument. it is used to give initial default state to the java objects at the moment it's getting initialize . Java Compiler injects default
constructor to the java byte code(Class File). If programmer writes parameterized constructor inside java file(Source File) then compiler will not inject Default Constructor.
Parameterized Constructor :-
A Parameterized Constructor is a constructor which takes one or more arguments. it is used to give initial state as per the business need to the java objects at the moment it's getting initialize .
Constructor Overloading :-
Constructor Overloading is a mechanism which allows to write more than one constructors inside a class.
Condition to overload a constructor:-
To overload a constructor either
Number of arguments should be differ
Type of arguments or order of arguments should be differ
Example of constructor overloading:-
Constructor Chaining In Java :- Constructor chaining in java allows to the programmer to call once constrcutor inside another constrcutor to reused the code for java object initialization process.
Some importants Points about Constructor :-
Constructor does not have return type
A constructor can not be final, abstract or synchronized.
Constructor can be overloaded
Constructor is not inherited.
Constructor can be chained
Call Constructor using this() and super().
Constructor should have same name as class in which it is defined.