Types of Constructors in Java
Now is the correct time to discuss the types of the constructor, so primarily there are three types of constructors in Java are mentioned below:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
1. Default Constructor in Java
A constructor that has no parameters is known as default the constructor. A default constructor is invisible. And if we write a constructor with no arguments, the compiler does not create a default constructor. It is taken out. It is being overloaded and called a parameterized constructor. The default constructor changed into the parameterized constructor. But Parameterized constructor can’t change the default constructor. The default constructor can be implicit or explicit. If we don’t define explicitly, we get an implicit default constructor. If we manually write a constructor, the implicit one is overridded.
Example:
// Java Program to demonstrate
// Default Constructor
import java.io.*;
// Driver class
class GFG {
// Default Constructor
GFG() { System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
GFG hello = new GFG();
}
}
Output
Default constructor
Note: Default constructor provides the default values to the object like 0, null, etc. depending on the type.
2. Parameterized Constructor in Java
A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.
Example:
// Java Program for Parameterized Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}
Output
GeekName :Avinash and GeekId :68
Remember: Does constructor return any value?
There are no “return value” statements in the constructor, but the constructor returns the current class instance. We can write ‘return’ inside a constructor.
Now the most important topic that comes into play is the strong incorporation of OOPS with constructors known as constructor overloading. Just like methods, we can overload constructors for creating objects in different ways. The compiler differentiates constructors on the basis of the number of parameters, types of parameters, and order of the parameters.
Example:
// Java Program to illustrate constructor overloading
// using same task (addition operation ) for different
// types of arguments.
import java.io.*;
class Geek {
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one "
+ "argument - String : " + name);
}
// constructor with two arguments
Geek(String name, int age)
{
System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}
// Constructor with one argument but with different
// type than previous..
Geek(long id)
{
System.out.println(
"Constructor with one argument : "
+ "Long : " + id);
}
}
class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
// Invoke the constructor with one argument of
// type 'String'.
Geek geek2 = new Geek("Shikhar");
// Invoke the constructor with two arguments
Geek geek3 = new Geek("Dharmesh", 26);
// Invoke the constructor with one argument of
// type 'Long'.
Geek geek4 = new Geek(325614567);
}
}
Output
Constructor with one argument - String : Shikhar Constructor with two arguments : String and Integer : Dharmesh 26 Constructor with one argument : Long : 325614567
3. Copy Constructor in Java
Unlike other constructors copy constructor is passed with another object which copies the data available from the passed object to the newly created object.
Note: In Java,there is no such inbuilt copy constructor available like in other programming languages such as C++, instead we can create our own copy constructor by passing the object of the same class to the other instance(object) of the class.
Example:
// Java Program for Copy Constructor
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
System.out.println();
// This would invoke the copy constructor.
Geek geek2 = new Geek(geek1);
System.out.println(
"Copy Constructor used Second Object");
System.out.println("GeekName :" + geek2.name
+ " and GeekId :" + geek2.id);
}
}
Output
First Object GeekName :Avinash and GeekId :68 Copy Constructor used Second Object GeekName :Avinash and GeekId :68
To know deep down about constructors there are two concepts been widely used as listed below:
Java Constructors
Java constructors or constructors in Java is a terminology used to construct something in our programs. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.