Java - Interview questions and Answers on Constructors

Q1.  Does Constructor creates the object ?

Ans. New operator in Java creates objects. Constructor is the later step in object creation. Constructor's job is to initialize the members after the object has reserved memory for itself.

Q2.  Does Java provides default copy constructor ?

Ans. No

Q3.  What are the common uses of "this" keyword in java ?

Ans. "this" keyword is a reference to the current object and can be used for following -

1. Passing itself to another method.
2. Referring to the instance variable when local variable has the same name.
3. Calling another constructor in constructor chaining.

Q4.  Can constructors be synchronized in Java ?

Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed.

Q5.  Are constructors inherited? Can a subclass call the parent's class constructor? When?

Ans. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to override the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

Q6.  Difference between object instantiation and construction ?

Ans. Though It's often confused with each other, Object Creation ( Instantiation ) and Initialization ( Construction ) are different things in Java. Construction follows object creation.

Object Creation is the process to create the object in memory and returning its handler. Java provides New keyword for object creation. 

Initialization is the process of setting the initial / default values to the members. Constructor is used for this purpose. If we don't provide any constructor, Java provides one default implementation to set the default values according to the member data types. 

Q7.  What is a Default Constructor ?

Ans. The no argument constructor provided by Java Compiler if no constructor is specified.

Q8.  Will Compiler creates a default no argument constructor if we specify only multi argument constructor ?

Ans. No, Compiler will create default constructor only if we don't specify any constructor.

Q9.  Can we overload constructors ?

Ans. Yes.

Q10.  What will happen if we make the constructor private ?

Ans. We can't create the objects directly by invoking new operator.

Q11.  How can we create objects if we make the constructor private ?

Ans. We can do so through a static public member method or static block.

Q12.  Can we use both "this" and "super" in a constructor ?

Ans. No, because both this and super should be the first statement.

Q13.  Can we instantiate the object of derived class if parent constructor is protected ?

Ans. No