Java Interview Questions on Constructors


In most of the Java interviews, the interviewers always start by asking basic questions. They can test one's knowledge in just a few minutes of the interviews. Therefore, it is essential to get thorough with the fundamental concepts of Java such as class, object and constructor. In this article, we are going to discuss interesting interview questions related to constructors. It will not just help to clear the job interviews but also improve one's knowledge as well as skills.

Java Interview Questions on Constructor

There could be numerous interview questions on Constructors, it is not possible to cover all in just one article. However, we have researched and assorted the most popular Java interview questions on Constructors.

In Java interviews, the first question one might encounter is to define constructors. So, let's start our discussion with this question.

What is Constructor?

A constructor is very similar to methods but the difference is that methods define the behavior of an object but a constructor is used to initialize those objects.

Do you know the rules for defining a Constructor?

Yes, here is the list of rules we need to follow while defining a constructor −

  • A constructor must have the same name as class name.

  • It can't have any return type.

  • We can associate public, private and protected access modifiers with a constructor.

  • The use of non-access modifiers like static and final are prohibited with constructors.

  • We can provide any number of parameters.

Difference between a method and a constructor

List of differences between methods and constructors −

  • The difference is that methods define the behavior of an object but constructor is used to initialize those objects.

  • We can provide any name of our choice to methods but a constructor must have the same name as class name.

  • Also, methods can return a value but the constructor does not return any value because they can't have any return type.

Can a constructor be defined as private and why?

Yes, we can define the constructor as private in Java to provide better security. If we use a private constructor for a class then we can restrict the object creation from outside the class scope. Singleton class is the best example for this case.

Explain the types of Constructors in Java

In Java, there are three types of constructors −

  • Default constructor − When we don't create any constructor, then the Java compiler will automatically create one we call it a default constructor.

  • Non-parameterized constructor − It is a constructor that we define explicitly without any parameter.

  • Parameterized constructor − It is a constructor that accepts parameters.

If we don't use any access modifier with constructor then what will be its default modifier?

Its default modifier will be the same as its class.

Is it possible to overload a constructor in Java?

Yes, it is possible to overload a constructor which means a class can have multiple constructors. Constructor overloading is a mechanism in which we define multiple constructors with the same name but with different parameters.

Define constructor chaining

Constructor chaining is a mechanism used to call one constructor from another constructor. It is used to perform multiple tasks one after another. To call the constructor of the same class, we use 'this' keyword. To call the constructor of the base class in sub class, we use 'super' keyword.

Why a static constructor is not allowed in Java?

The member of class defined using static belongs to the class only. But, a constructor is always invoked when we create an instance of class. Therefore, a class cannot have a static constructor.

Why an abstract constructor is not allowed in Java?

An abstract method lacks the method implementation and a constructor is similar to a method, but it cannot lack an implementation. Hence, a class cannot have an abstract constructor.

Conclusion

In this article, we understood the importance of constructors in Java based interviews. We have covered the most popular and important interview questions on constructors. It is one of the fundamental concepts of Java that plays a vital role in Java application development.

Updated on: 10-Aug-2023

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements