Constructors in Java
Key Points:-
1. Definition of constructor?
2. Types of Constructor
3. Constructor overloading
4. Difference between constructor and Method
A constructor is a special method that is used to initialize an
object.Every class has a constructor,if we don't explicitly declare a
constructor for any java class the compiler builds a default constructor
for that class. A constructor does not have any return type.
A constructor has same name as the class in which it resides.
Constructor in Java can not be abstract, static, final or synchronized.
These modifiers are not allowed for constructor.
For example:
Employee emp = new Employee();
Here, Employee() is invoking a default constructor.
Types of Constructor
There are two types of constructors:
• Default Constructor
• Parameterized constructor
• Copy constructor
Each time a new object is created at least one constructor will be
invoked.
Default Constructor
In Java, a constructor is said to be default constructor if it does not have
any parameter. There can be two types of constructor i.e System Define
default constructor or user defines default constructor. If a class does
not contain any constructor then during compilation the system
generates a default constructor which is known as system define default
constructor. If a class contain a constructor with no parameter then it is
known as default constructor
User Define Default Constructor
Example:
Parameterised constructor
A Parameterised constructor receives parameters at the time of
creating an object and initializes the object with the received
values.
If we define only parameterized constructors, then we cannot create an
object with default constructor. This is because compiler will not create
default constructor. You need to create default constructor explicitly.
Example:-
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x = i;
You would call constructor to initialize objects as follows −
public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + " " + t2.x);
Copy constructor:
A copy constructor is a constructor that creates a new object using an
existing object of the same class and initializes each instance variable of
newly created object with corresponding instance variables of the existing
object passed as argument. This constructor takes a single argument
whose type is that of the class containing the constructor. A system
provides its own copy constructor if no copy constructor is defined.
class Rectangle
{
int length;
int breadth;
//constructor to initialize length and bredth of rectang of rectangle
Rectangle(int l, int b)
{
length = l;
breadth= b;
}
//copy constructor
Rectangle(Rectangle obj)
{
System.out.println("Copy Constructor Invoked");
length = obj.length;
breadth= obj.breadth;
}
//method to calcuate area of rectangle
int area()
{
return (length * breadth);
}
}
//class to create Rectangle object and calculate area
class CopyConstructor
{
public static void main(String[] args)
{
Rectangle firstRect = new Rectangle(5,6);
Rectangle secondRect= new Rectangle(firstRect);
System.out.println("Area of First Rectangle : "+
firstRect.area());
System .out.println("Area of First Second Rectangle : "+
secondRect.area());
}
}
Output:-
Copy constructor Invoked
Area of First Rectangle :30
Area of First Second Rectangle :30
Example-2
class pnb
{
private int sa;
public pnb()
{}
public pnb(int x)
sa=x;
public pnb(pnb x)
this.sa=x.sa;
public void display()
System.out.println("Saving Account = "+sa);
class CopyDemo1
public static void main(String as[])
pnb obj = new pnb(2000);
pnb obj1 = new pnb(obj);
pnb obj2 = new pnb();
System.out.println("object ");
obj.display();
System.out.println("object 1");
obj1.display();
System.out.println("object 2");
obj2.display();
System.out.println("System provide its own copy constructor ==>
object 2");
obj2=obj;
obj2.display();
Constructor overloading:-
The process of using a number of constructors with the same
name but different types of parameters is known as Constructor
overloading. Sometimes there is a need of initializing an object in
different ways.
Example:-
// Java program to illustrate
// Constructor Overloading
class Box
{
double width, height, depth;
// constructor used when all dimensions
// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions
// specified
Box()
{
width = height = depth = 0;
}
// constructor used when cube is created
Box(double len)
{
width = height = depth = len;
}
// compute and return volume
double volume()
{
return width * height * depth;
}
}
// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);
// get volume of cube
vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
Output:
Volume of mybox1 is 3000.0
Volume of mybox2 is 0.0
Volume of mycube is 343.0
Difference between constructor and method.
Constructor Method
It is a group of statements that can
It is a block of code that
be called at any point in the program
initializes a newly created
using its name to perform a specific
object.
task.
It has the same name as It should have a different name than
class name. class name.
It needs a valid return type if it
It has no return type
returns a value otherwise void
It is called implicitly at the It is called explicitly by the
time of object creation programmer by making a method call
If a constructor is not
present, a default In case of a method, no default
constructor is provided by method is provided.
Java
It is not inherited by It may or may not be inherited
subclasses. depending upon its access specifier.