Java Interview Questions on Constructors

28 Jun, 2021
What is a Constructor?Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.Do we have Copy Constructor in Java?Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.To copy the values of one object into another in java, you can use:ConstructorAssigning the values of one object into anotherclone() method of Object classWhat is Constructor Chaining ?Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.// Java program to illustrate Constructor Chaining// within same class Using this() keywordclass Temp{    // default constructor 1    // default constructor will call another constructor    // using this keyword from same class    Temp()    {        // calls constructor 2        this(5);        System.out.println("The Default constructor");    }      // parameterized constructor 2    Temp(int x)    {        // calls constructor 3        this(5, 15);        System.out.println(x);    }      // parameterized constructor 3    Temp(int x, int y)    {        System.out.println(x * y);    }      public static void main(String args[])    {        // invokes default constructor first        new Temp();    }}Can we call sub class constructor from super class constructor?No. There is no way in java to call sub class constructor from a super class constructor.What happens if you keep a return type for a constructor?Ideally, Constructor must not have a return type. By definition, if a method has a return type, it’s not a constructor.(JLS8.8 Declaration) It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.Example:class GfG
{
    int GfG()
    {
        return 0;    // Warning for the return type
    }
}What is No-arg constructor?Constructor without arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.
class GfG
{
    public GfG()
    {
        //No-arg constructor
    }
}How a no – argument constructor is different from default Constructor?If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.What are private constructors and where are they used?Like any method we can provide access specifier to the constructor. If it’s made private, then it can only be accessed inside the class.The major scenarios where we use private constructor:Internal Constructor chainingSingleton class design patternWhen do we need Constructor Overloading?Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. Different constructors can do different work by implementing different line of codes and are called based on the type and no of parameters passed.According to the situation , a constructor is called with specific number of parameters among overloaded constructors.Do we have destructors in Java?No, Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.
What is a Constructor?Constructors are used to initialize the object’s state. Like methods, a constructor also contains collection of statements(i.e. instructions) that are executed at time of Object creation.
Do we have Copy Constructor in Java?Like C++, Java also supports copy constructor. But, unlike C++, Java doesn’t create a default copy constructor if you don’t write your own.To copy the values of one object into another in java, you can use:ConstructorAssigning the values of one object into anotherclone() method of Object class
Constructor
Assigning the values of one object into another
clone() method of Object class
What is Constructor Chaining ?Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.// Java program to illustrate Constructor Chaining// within same class Using this() keywordclass Temp{    // default constructor 1    // default constructor will call another constructor    // using this keyword from same class    Temp()    {        // calls constructor 2        this(5);        System.out.println("The Default constructor");    }      // parameterized constructor 2    Temp(int x)    {        // calls constructor 3        this(5, 15);        System.out.println(x);    }      // parameterized constructor 3    Temp(int x, int y)    {        System.out.println(x * y);    }      public static void main(String args[])    {        // invokes default constructor first        new Temp();    }}
// Java program to illustrate Constructor Chaining// within same class Using this() keywordclass Temp{    // default constructor 1    // default constructor will call another constructor    // using this keyword from same class    Temp()    {        // calls constructor 2        this(5);        System.out.println("The Default constructor");    }      // parameterized constructor 2    Temp(int x)    {        // calls constructor 3        this(5, 15);        System.out.println(x);    }      // parameterized constructor 3    Temp(int x, int y)    {        System.out.println(x * y);    }      public static void main(String args[])    {        // invokes default constructor first        new Temp();    }}
Can we call sub class constructor from super class constructor?No. There is no way in java to call sub class constructor from a super class constructor.
What happens if you keep a return type for a constructor?Ideally, Constructor must not have a return type. By definition, if a method has a return type, it’s not a constructor.(JLS8.8 Declaration) It will be treated as a normal method. But compiler gives a warning saying that method has a constructor name.Example:class GfG
{
    int GfG()
    {
        return 0;    // Warning for the return type
    }
}
class GfG
{
    int GfG()
    {
        return 0;    // Warning for the return type
    }
}
What is No-arg constructor?Constructor without arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.
class GfG
{
    public GfG()
    {
        //No-arg constructor
    }
}

class GfG
{
    public GfG()
    {
        //No-arg constructor
    }
}
How a no – argument constructor is different from default Constructor?If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
What are private constructors and where are they used?Like any method we can provide access specifier to the constructor. If it’s made private, then it can only be accessed inside the class.The major scenarios where we use private constructor:Internal Constructor chainingSingleton class design pattern
Internal Constructor chaining
Singleton class design pattern
When do we need Constructor Overloading?Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. Different constructors can do different work by implementing different line of codes and are called based on the type and no of parameters passed.According to the situation , a constructor is called with specific number of parameters among overloaded constructors.
Do we have destructors in Java?No, Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor.
Quiz on Constructors
This article is contributed by Kiana Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
interview-preparation
placement preparation
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java
For-each loop in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java