Java Constructor Interview Questions: In this article, we will cover some of the interview questions with their justification for Java Constructor. These are most frequently asked interview question from OOPS concepts
Java Constructor
What is Constructor in Java?
- It 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 provides values at the time of object creation that is why it is called as a constructor (constructing default/initial values for an object)
Define Constructor in Java?
- The 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
Is it mandatory to define a constructor in a class?
- It is not mandatory to define a constructor in class because the compiler inserts a default no-arg constructor during compilation
- Note: compiler inserts default no-arg constructor when there is no constructor explicitly defined by a programmer in class
Check: Abstract Class In Java
What are the rules for defining constructors in a class?
- Name of the constructor should be the same as that of a class name
- A constructor doesn’t have any return type unlike methods (not even void)
Generally, what is the name of the constructor in Java class and why it is so?
- The name of the constructor should be (must be) same as that of a class name
- It is just syntax or convention followed in Java and requires no extra keyword
Default constructor & Parametrized constructor
What are the types of constructors in Java?
There are two types of constructor
- Default constructor (no-arg constructor)
- Parameterized constructor
What is a no-arg constructor?
- Constructor which takes zero parameters is called as default constructor
- Or, a constructor with no argument is known as a default constructor
- It is also known as no-arg constructor
What is the default constructor and why it is called the default constructor? Reasons?
- Constructor which takes zero parameters is called as default constructor
- The compiler always inserts a no-arg constructor during compilation process if there is no other constructor defined explicitly by the programmer
- During such a compilation process, compiler initializes all instance data members to default values like
0 for int
null for String
false for Boolean - Since it provides default values, it is alternatively called as default constructor (other is a no-argument constructor)
What is parametrized, constructor?
- Constructor which takes one or more parameters is called as a parameterized constructor
- Or, a constructor with arguments is known as parameterized constructor
What happens if we don’t specify any constructor explicitly in class?
- If there is no constructor defined explicitly by the programmer, the compiler inserts a default no-arg constructor during compilation
When compiler provides a default, constructor?
- If there is no constructor defined explicitly by a programmer, the compiler inserts a default no-arg constructor during compilation
Will the compiler provides a default no-argument constructor when we explicitly defined a constructor in a class?
- The compiler won’t provide/inserts default no-arg constructor during compilation process if a programmer defines constructor explicitly (whether it is default or parametrized constructor)
Will compiler provides default no-argument constructor when we explicitly defined a parameterized constructor in a class?
- The compiler won’t provide/inserts default no-arg constructor during compilation process if a programmer defines constructor explicitly (whether it is default or parametrized constructor)
What happens when there is one parameterized constructor explicitly defined?
- Compilation succeeds
- But, if we try to create a new object using default constructor then compiler throws an error
If we define a constructor in a class, then will it have a default constructor?
- No, there won’t be any default constructor
- The programmer needs to define explicitly if required
Can we have both default constructors and parameterized constructors in Java class?
- Yes, constructor overloading is possible
Constructor on the return type
Why return type is not allowed for the constructor?
- When we add a return type to a constructor, the compiler treats this as a method with method name same as that of the class name
- Error: Compilation error will be thrown
Whether class compiles successfully if we add return-type to a constructor?
- A compilation error will be thrown
- To be precise, if we add return-type then compiler treats this as a method with method name same as that of the class name
Can constructor return any value, although there is no return type?
- As such, there is no return type for the constructor and it doesn’t return values
- But constructor return values in the form of instances after initialization
Private Constructor
What is a private constructor in Java?
- Adding a private keyword to constructor makes constructor as a private constructor
- Which implies except its own class, no other classes are not allowed to instantiate objects of this type
Can we add access to the modifier ‘private’ to the constructor?
- Yes, very well
- Access modifiers like private, protected and the public are allowed (even default is ok)
- the private constructor is meant to create a singleton object (singleton design pattern)
Read Also: Data Structure Latest Interview Question
How can we create objects, if we make the constructor as private?
- Add a private keyword to the constructor which becomes a private constructor
- By making, constructor as private makes difficult for other classes to instantiate objects of this type
Can we declare constructor as ‘protected’?
- Yes, we can declare constructor as protected
Can we instantiate a subclass object, if superclass constructor defined is protected?
- Yes, we can create an object of subclass type even if super class’s constructor is marked as protected
Constructor on non-access modifier:
Can a constructor be final?
- No, we cannot mark constructor as final
- non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in the constructor
Q) Is it valid to add ‘final’ to a constructor in Java? (Non-access modifier)
- The final keyword is not allowed in the constructor
- non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in the constructor
Static Constructor
Explain static constructor in Java?
- There is no such thing known as a Static constructor in Java
Can we declare constructor as ‘static’?
- non-access modifiers like final, static, transient, synchronized, volatile, strictfp are not allowed in the constructor
Constructor Overloading
Can we overload constructor in Java?
- Yes, constructor overloading is possible
Why do we overload constructor?
- Constructor provides a way to create an object implicitly for any class using ‘new’ keyword
- So, overloaded constructor serves in many ways to create distinct objects using different types of data of the same class
Is overloading constructor an example of both polymorphism and inheritance?
- Constructor provides a way to create a 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, the constructor cannot be inherited rather it can access via constructor chaining process
How JVM differentiates between constructor and methods in Java during compilation?
- With the help of return type
- The constructor is a special type of method which has no return type
Difference between constructor and methods in Java?
Constructor Chaining
Explain Constructor chaining in Java?
Explain this() keyword w.r.t constructor in Java?
- To invoke one constructor from another constructor, we use this() constructor call
How to invoke one constructor from another constructor in the same class?
- Using this() constructor call
Explain super() keyword w.r.t constructor in Java?
- To invoke super class’s constructor from subclass constructor, we use super() constructor call
How to invoke superclass constructor from subclass constructor?
- Using super() constructor call
Is it possible to call subclass constructor from the superclass constructor?
- No, it is not possible
- Using super() constructor call, we can invoke super class’s constructor from subclass constructor but reversely is not possible
Can we have both this() and super() inside the same constructor?
- No, at any given point in 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 the 1st statement of a constructor
Constructor on abstract classes and Interface
Can an abstract class have a constructor in Java?
- Yes, defining constructor inside abstract classes is allowed
- But the object of the abstract class cannot be instantiated rather we can use this as reference variable (rather inheritance concept)
Can an Interface have a 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 the default method and static method inside Java Interface
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
References: Link
Leave a Reply