Constructor Interview Questions In Java: A constructor is a special method in Java that initializes objects of a class. Java Constructor Interview Questions evaluate a candidate’s knowledge and proficiency in using constructors in Java programming. These interview questions assess a candidate’s understanding of constructors, including their types, purpose, and best practices.
Java Constructor Interview Questions require candidates to demonstrate their expertise in creating and using constructors to initialize objects of a class, with proper exception handling and encapsulation. This article presents some commonly asked Java Constructor Interview Questions to help Java developers prepare for interviews and have a comprehensive understanding of constructors in Java programming.
Constructor Interview Questions In Java
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 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
What are the rules for defining constructors in a class?
- The 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 is it 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
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 that 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 is it called the default constructor? Reasons?
- Constructor that takes zero parameters is called as default constructor
- The compiler always inserts a no-arg constructor during the compilation process if there is no other constructor defined explicitly by the programmer
- During such a compilation process, the 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 a default constructor (the other is a no-argument constructor)
What is a parametrized constructor?
- A constructor which takes one or more parameters is called 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 the 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 provide a default no-argument constructor when we explicitly define a constructor in a class?
- The compiler won’t provide/insert a default no-arg constructor during the compilation process if a programmer defines the constructor explicitly (whether it is a default or parametrized constructor)
Will the compiler provide a default no-argument constructor when we explicitly define a parameterized constructor in a class?
- The compiler won’t provide/insert a default no-arg constructor during the compilation process if a programmer defines the constructor explicitly (whether it is a 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 the default constructor, then the 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 the Java class?
- Yes, constructor overloading is possible
Why is the return type not allowed for the constructor?
- When we add a return type to a constructor, the compiler treats this as a method with a method name the 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 the compiler treats this as a method with a method name the same as that of the class name
Can the 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 the constructor returns values in the form of instances after initialization
Private Constructor
What is a private constructor in Java?
- Adding a private keyword to the constructor makes the constructor a private constructor
- This implies except for 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 public are allowed (even default is ok)
- the private constructor is meant to create a singleton object (singleton design pattern)
How can we create objects if we make the constructor private?
- Add a private keyword to the constructor, which becomes a private constructor
- By making, constructor as private makes it difficult for other classes to instantiate objects of this type
Can we declare the constructor as ‘protected’?
- Yes, we can declare the constructor as protected
Can we instantiate a subclass object if the superclass constructor defined is protected?
- Yes, we can create an object of subclass type even if the super class’s constructor is marked as protected
Constructor on non-access modifier:
Can a constructor be final?
- No, we cannot mark the constructor as final
- non-access modifiers like final, static, transient, synchronized, volatile, and 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, and strictfp are not allowed in the constructor
Explain static constructors in Java.
- There is no such thing as a Static constructor in Java
Can we declare the constructor as ‘static’?
- non-access modifiers like final, static, transient, synchronized, volatile, and strictfp are not allowed in the constructor
Can we overload the constructor in Java?
- Yes, constructor overloading is possible
Why do we overload constructors?
- Constructor provides a way to create an object implicitly for any class using the ‘new’ keyword
- So, an 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 be accessed via the constructor chaining process
How does JVM differentiate between constructors 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
What is the difference between constructors and methods in Java?
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 the super() keyword w.r.t constructor in Java.
- To invoke the super class’s constructor from the subclass constructor, we use the super() constructor call
How to invoke a superclass constructor from a subclass constructor?
- Using super() constructor call
Is it possible to call a subclass constructor from the superclass constructor?
- No, it is not possible
- Using the super() constructor call, we can invoke the super class’s constructor from the 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
Can an abstract class have a constructor in Java?
- Yes, defining constructors inside abstract classes is allowed
- But the object of the abstract class cannot be instantiated rather, we can use this as a reference variable (rather inheritance concept)
Can an Interface have a constructor in Java?
- defining constructors inside the 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 the Java Interface
Does Java provide a 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 the below example
References: Link