Java Constructor Interview Questions
A constructor is an integral part of a Java program. It is one of the important topics of core Java. So, in every Java-based interview, there is a possibility that the interviewer may ask few questions from the Java constructor.
In this article, we are going to discuss some commonly asked interview questions on constructors.
1) Define Constructor?
Java constructor is a unique method that initializes the objects, which is called when an instance of the class is created. The memory for the object is allocated when we call the constructor.
Basically, a constructor is a block of code. When we create an object of the class using the new() keyword, at least one constructor is called, and it initializes the objects and allocates memory to them.
If we do not specify any constructor, it will call the default constructor of the class. However, it is not necessary to specify an explicit constructor because the Java compiler provides a default constructor for every Java class.
2) How many types of Constructors are in Java?
There are two types of constructors in Java:
The syntax for the default constructor is as follows:
The syntax for the parameterized constructor is as follows:
3) Do we have a copy constructor in Java?
Unlike C++, there is no explicit copy constructor in Java. However, we can achieve the functionality of a copy constructor in Java by copying the values from one object to another, just like the copy constructor.
The following are some methods to copy the values from one object to another:
4) Write a Java Program to Copy the values from one object to another Object.
Below Java program copies the values from one object to another object:
100 Joy 100 Joy
In the above example, we have created two instances of the ConstructorDemo and passed the first object value into the second constructor. This way, we can use a copy constructor in Java.
5) Is there any method to call a sub-class constructor from a superclass constructor?
The subclass constructor has its own private data members, so Java does not provide any way to access the sub-class constructor from a super class constructor. However, we can call a superclass constructor from a sub-class constructor by using the super keyword.
6) Can we have a constructor in the Interface?
No, we cannot have constructors in the Java interface.
7) Explain Constructor Chaining?
Constructor Chaining is a way to call one constructor from another constructor with respect to the current object. It can be achieved in the following two ways:
From base class: We can use the super keyword to call a constructor from the base class.
Within the same class: We can call a constructor within the same class by using this() keyword.
Below is an example of constructor chaining:
8) What happens if we provide a return type to a constructor?
If we provide a return type to a constructor, it will function as a general method. But, the compiler will display a warning message, "This method has a Constructor name".
Consider the below example:
The above program will be compiled gracefully, but it displays below warning message:
9) What is a private constructor?
Like methods, we can have the private constructors in Java. To make or create a constructor as private, use the private keyword while declaring it. It can only be accessed within that class.
The following are some usage scenarios when we need a private constructor:
Below is an example of the private constructor:
In a private constructor
10) Why constructors in Java cannot be static?
The constructors cannot be static in Java. When we declare a method as static, it means the method belongs to the class and not to a specific object. But the constructor is always invoked to the reference of objects. So, there is no sense in making a constructor static.
11) Can we make a constructor final?
No, we cannot make a constructor final. If we made a constructor final, it would throw a compile-time error "modifier final not allowed".
12) Can we make a constructor abstract?
a body, which really makes no sense. It is automatically called at the time of object creation. So, it cannot be a block without a body.
13) what will happen when a constructor is declared as protected?
Generally, when we declare a method as protected, other classes can access that method in a different package by using inheritance only. But, when we declare a constructor protected, it behaves slightly differently than a method. The protected constructor can only be accessed by using a super keyword according to Java language standards.
14) Why constructor name is similar to the class name?
When we create an object of a class using a new keyword, it should have information about that particular class. That is why the constructor's name must be similar to the class name.
15) Why return type is not allowed for the constructor?
The return type is not allowed in the constructor because if we provide a return type in the constructor, it will act as the normal method. So, to differentiate between constructor and method block, the return type is not allowed in constructors.