Interview Question and Answers on Java Constructor

In this article, we will cover some of the interview questions with their justification on Java Constructor

These are most frequently asked interview question from OOPS concepts

Java Constructor:

Q) What is Constructor in Java?

–> Constructor is a special type of method that is used to initialize an object

–> Every class has a constructor which is invoked at the time of object creation and provides values

–> As this provide values at the time of object creation that is why it is called as constructor (constructing default/initial values for object)

Q) Define Constructor in Java?

–> Constructor is a special type of method that is used to initialize an object. Every class has a constructor which is invoked at the time of object creation and provides values

Q) Is it mandatory to define constructor in class?

–> It is not mandatory to define constructor in class because compiler inserts a default no-arg constructor during compilation

–> Note: compiler inserts default no-arg constructor when there is no constructor explicitly defined by programmer in class

Q) What are the rules for defining constructor in a class?

–> Name of the constructor should be same as that of class name

–> Constructor don’t have any return type unlike methods (not even void)

–> Read more about <Java Constructor> rules here

Q) Generally, what is the name of constructor in Java class and why it is so?

–> The name of constructor should be (must be) same as that of class name

–> It is just a syntax or convention followed in Java and requires no extra keyword

 

Default constructor && Parametrized constructor:

Q) What are the types of constructor in Java?

–> There are two types constructor

  • Default constructor (no-arg constructor)
  • Parametrized constructor

Q) What is no-arg constructor?

–> Constructor which takes zero parameter is called as default constructor

–> Or, constructor with no argument is known as default constructor

–> It is also known as no-arg constructor

Q) What is default constructor and why it is called default constructor. Reasons?

–> Constructor which takes zero parameter is called as default constructor

–> Compiler always inserts a no-arg constructor during compilation process if there is mo other constructor defined explicitly by programmer

–> During such compilation process, compiler initializes all instance data members to default values like

  • 0 for int
  • null for String
  • false for Boolean
  • etc

–> Since it is provides default values, it is alternatively called as default constructor (other is no-argument constructor)

Q) What is parametrized constructor?

–> Constructor which takes one or more parameters is called as parametrized constructor

–> Or, constructor with arguments is known as parametrized constructor

Q) no-argument constructor vs parametrized constructor in Java?

–> Read Default constructor v/s Parametrized constructor

Q) What happens, if we don’t specify any constructor explicitly in class?

–> If there is no constructor defined explicitly by programmer, then compiler inserts a default no-arg constructor during compilation

Q) When compiler provides default constructor?

–> If there is no constructor defined explicitly by programmer, then compiler inserts a default no-arg constructor during compilation

Q) Will compiler provides default no-argument constructor, when we explicitly defined constructor in class?

–> Compiler won’t provide/inserts default no-arg constructor during compilation process, if programmer defines constructor explicitly (whether it is default or parametrized constructor)

Q) Will compiler provides default no-argument constructor, when we explicitly defined parametrized constructor in class?

–> Compiler won’t provide/inserts default no-arg constructor during compilation process, if programmer defines constructor explicitly (whether it is default or parametrized constructor)

Q) What happens, when there is one parametrized constructor explicitly defined?

–> Compilation succeeds

–> But, if we try to create new object using default constructor then compiler throws error

–> Error:

Q) If we define a constructor in class, then will it have default constructor?

–> No, there won’t be any default constructor

–> Programmer needs to define explicitly, if required

Q) Can we have both default constructor and parametrized constructor in Java class?

–> Yes, constructor overloading is possible

–> Read constructor overloading concept in detail

 

Constructor on return type:

Q) Why return type is not allowed for constructor?

–> When we add return type to constructor, then compiler treats this as method with method name same as that of class name

–> Error: Compilation error will be thrown

Q) Whether class compiles successfully, if we add return-type to constructor?

–> Compilation error will be thrown

–> To be precise, if we add return-type then compiler treats this as method with method name same as that of class name

Q) Can constructor return any value, although there is no return type?

–> As such there is no return type for constructor and it doesn’t return values

–> But constructor return values in the form of instances after initialization

 

Private Constructor:

Q) What is private constructor in Java?

–> Adding private keyword to constructor makes constructor as private constructor

–> Which implies except its own class, no other classes are not allowed to instantiate objects of this type

–> Read private constructor in detail

Q) Can we add access modifier ‘private’ to constructor?

–> Yes, very well

–> Access modifiers like private, protected and public are allowed (even default is ok)

–> private constructor are mean to create singleton object (singleton design pattern)

Q) How can we create objects, if we make the constructor as private?

–> Add private keyword to constructor which becomes private constructor

–> By making constructor as private makes difficult for other classes to instantiate objects of this type

–> Read private constructor in detail

Q) Can we declare constructor as ‘protected’?

–> Yes, we can declare constructor as protected

Q) Can we instantiate sub class object, if super class constructor defined is protected?

–> Yes, we can create object of sub class type even if super class’s constructor is marked as protected

 

Constructor on non-access modifier:

Q) Can constructor be final?

–> No, we cannot mark constructor as final

–> Non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in constructor

Q) Is it valid to add ‘final’ to constructor in Java? (Non-access modifier)

–> final keyword is not allowed in constructor

–> Non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in constructor

 

Static Constructor:

Q) Explain static constructor in Java?

–> There is no such thing known as Static constructor in Java

–> Read more about <static constructor> in detail

Q) Can we declare constructor as ‘static’?

–> Non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in constructor

–> Read more about <static constructor> in detail

 

Constructor Overloading:

Q) Can we overload constructor in Java?

–> Yes, constructor overloading is possible

–> Read more about <constructor overloading> in detail

Q) Why do we overload constructor?

–> Constructor provides a way to create object implicitly of any class using ‘new’ keyword

–> So, overloaded constructor serves many ways to create distinct objects using different types of data of same class

–> Read more about <constructor overloading> in detail

Q) Is overloading constructor an example of both polymorphism and inheritance?

–> Constructor provides a way to create distinct object using different types of data

–> Polymorphism is achieved through method overloading (static polymorphism) and method overriding (dynamic polymorphism) but not with constructor overloading

–> Also, constructor cannot be inherited rather it can accessed via constructor chaining process

 

Constructor v/s Methods

Q) How JVM differentiates between constructor and methods in Java during compilation?

–> With the help of return type

–> Constructor is a special type of method which has no return type

–> Read more about <Constructor v/s Method> in detail

Q) Difference between constructor and methods in Java?

–> Read more about <Constructor v/s Method> in detail

 

Constructor Chaining:

Q) Explain Constructor chaining in Java?

–> Read more about <constructor chaining> in detail

Q) Explain this() keyword w.r.t constructor in Java?

–> To invoke one constructor from another constructor, we use this() constructor call

–> Read constructor chaining in detail

Q) How to invoke one constructor from another constructor in same class?

–> Using this() constructor call

–> Read constructor chaining in detail for understanding this invocation

Q) Explain super() keyword w.r.t constructor in Java?

–> To invoke super class’s constructor from sub class constructor, we use super() constructor call

–> Read constructor chaining in detail

Q) How to invoke super class constructor from sub class constructor?

–> Using super() constructor call

–> Read constructor chaining in detail for understanding this invocation

Q) Is it possible to call sub class constructor from super class constructor?

–> No, it is possible

–> Using super() constructor call, we can invoke super class’s constructor from sub class constructor but reverse is not possible

Q) Can we have both this() and super() inside the same constructor?

–> No, at any given point of time both this() and super cannot be present inside the same constructor

–> Either this() constructor call or super() constructor call is allowed, if present should be 1st statement of constructor

 

Constructor on abstract classes and Interface:

Q) Can an abstract class have constructor in Java?

–> Yes, defining constructor inside abstract classes is allowed

–> But object of abstract class cannot be instantiated rather we can use this as reference variable (rather inheritance concept)

Q) Can an Interface have constructor in Java?

–> Defining constructors inside Interface is not allowed and compilation is thrown

–> Starting Java 8, new features are added but it doesn’t allow defining constructor

–> One such feature is default method and static method inside Java Interface

Q) Does Java provide default copy constructor?

–> Java doesn’t have any built-in copy constructor

–> But, yes programmer can write their own copy constructor in their own way, like below example

–> Example coming up

 

References

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

 

Read Also:

Happy Coding !!
Happy Learning !!