There are 3 types of constructor in java
Default constructor
Parameterized constructor
Parameterless constructor
Default constructor
If we do not define any constructor then compiler generates one constructor for class is called default constructor.
Compiler generated default constructor is parameterless.
Compiler never generate default parameterized constructor, if we want to define instance of a class by passing arguments then we must define parameterized constructor inside class
Parameterized Constructor
If we define constructor of a class having one or more parameter then it is called parameterized constructor.
When we create instance of a class by passing arguments then matching parameterized constructor gets called.
Example:
public Employee(int empId,String empName,String companyName)
{
}
Employee employee=new Employee(123,”bob”,”Vodafone”)
ParameterlessConstructor
If we define constructor of a class without any parameter then it is called parameterless constructor or zero argument constructor.
When we create instance of a class without passing any argument then parameterless constructor of a class get called.
It is also called as zero argument constructor or user defined default constructor.
Example:
public Employee(){ }
Employee employee=new Employee()
Comments
Post a Comment