Constructor
Constructor
Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary
to write a constructor for a class. It is because java compiler creates a default constructor if your class doesn't
have any.
Rules for creating Java constructor
• There are two rules defined for the constructor.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and synchronized
Default
constructor
int empId;
• Constructor with String empName;
Constructor overloading
is a concept of having
more than one
constructor with different
parameters list, in such a
way so that each
constructor performs a
different task.
In this example, we are creating the no-arg constructor in the Bike class. It will be
invoked at the time of object creation.