Google News
logo
Java Constructor Interview Questions
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.
There are two types of constructors in Java :
 
* Default Constructor (Non-parameterized Constructor)
* Parameterized Constructor

The syntax for the default constructor is as follows :
 
<class name>() {}  

Example :
Employee()  
{  
//some code  
} 
 
The syntax for the parameterized constructor is as follows :
 
<class name>(arg1, arg2) {}  

Example :
Employee(int i, String n)  
{      
    id = i;    
    name = n;    
} ​

 

 
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 :

* Constructor
* Assigning the values of one object into another
* clone() method of Object class
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() keyword
class 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();
    }
}

 

No. There is no way in java to call sub class constructor from a super class 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 FTL
{
    int FTL()
    {
        return 0;    // Warning for the return type
    }
}​

 

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 :
 
* Internal Constructor chaining
* Singleton class design pattern
* Below is an example of the private constructor:
 
PrivateConstructor.java :
 
class SingletonDemo {  
   private SingletonDemo() {  
      System. out.println("In a private constructor");  
   }  
   public static SingletonDemo getObject() {  
      // we can call this constructor  
      if (ref == null)  
         ref = new SingletonDemo();  
      return ref;  
   }  
   private static SingletonDemo ref;  
}  
public class PrivateConstructor {  
   public static void main(String args[]) {  
      SingletonDemo sObj = SingletonDemo.getObject();  
   }  
}​

 

Output :
 
In a private constructor

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.
Constructor without arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.
 
class FTL
{
    public FTL()
    {
        //No-arg 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.
No, we cannot make a constructor final. If we made a constructor final, it would throw a compile-time error "modifier final not allowed".
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.
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.
* 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.
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.
As developers and software engineers, our aim is to always design ways to obtain maximum efficiency and if we need to write less code for it, then that’s a blessing.
 
In Java, a record is a special type of class declaration aimed at reducing the boilerplate code. Java records were introduced with the intention to be used as a fast way to create data carrier classes, i.e. the classes whose objective is to simply contain data and carry it between modules, also known as POJOs (Plain Old Java Objects) and DTOs (Data Transfer Objects). Record was introduced in Java SE 14 as a preview feature, which is a feature whose design, implementation, and specification are complete but it is not a permanent addition to the language, which means that the feature may or may not exist in the future versions of the language. Java SE 15 extends the preview feature with additional capabilities such as local record classes.
 
Let us first do discuss why do we need records prior to implementing them. Let us consider an illustration for this.
When you set a method as abstract, then “The method doesn’t or cannot have body”. A constructor will be automatically called when object is created. It cannot lack a body moreover an abstract constructor could never be implemented.
When you set a method as final, then” The method cannot be overridden by any class”, but Constructor by JLS ( Java Language Specification ) definition can’t be overridden. A constructor is not inherited, so there is no need for declaring it as final.
Because it obtain all its default properties from its class.
 
They are :
 
* Its accessibility modifier is same as its class accessibility modifier 
* Its name is same as class name.
* Its does not have parameters and logic.