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
1. 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 which provides initial values
- As it provides value at the time of object creation that is why it is called as constructor (constructing object with default/initial values)
Q) Define Constructor in Java ?
- Constructor is a special type of method which is used to initialize an Object
- Every class has a constructor which is invoked at the time of object creation which provides initial values
Q) Is it mandatory to define constructor in a class ?
- It is not mandatory to define constructor in a class because compiler inserts a default no-arg constructor during compilation
- Note: compiler inserts default no-argument 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 (or must be) same as that of class name
- It is a special syntax or convention followed in Java and requires no additional keyword
2. Default constructor && Parameterized constructor:
Q) What are the types of constructor in Java ?
- There are two types constructor
- Default constructor (no-argument constructor)
- Parameterized constructor
Q) What is no-arg constructor ?
- Constructor which takes zero parameter as input argument is called as default constructor
- Or, constructor with no argument is known as default constructor
- It is also known as no-arg constructor or no-argument constructor
Q) What is default constructor and why it is called default constructor. Reasons ?
- Constructor which takes zero parameter as input argument is known as default constructor
- Compiler always inserts a no-arg constructor during compilation process if there is no other constructor defined explicitly by programmer
- During such compilation process, compiler initializes all instance member variables to default values like
- 0 for int
- null for String
- false for Boolean
- etc.
- Since it provides default values, it is alternatively known as default constructor (or no-argument constructor)
Q) What is parameterized constructor ?
- Constructor which takes one or more parameters as input-arguments is known as parameterized constructor
- Or, constructor with arguments is known as parameterized constructor
Q) no-argument constructor vs parameterized constructor in Java ?
Q) What happens, if we don’t specify any constructor explicitly in a class ?
- If there is no constructor defined explicitly by programmer, then compiler inserts a default no-arg constructor during compilation of a class
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 of a class
Q) Will compiler provides default no-argument constructor, when we explicitly define constructor in a class ?
- Compiler won’t provide/inserts default no-arg constructor during compilation process, if programmer defines constructor explicitly (whether it is either default constructor or parameterized constructor)
Q) Will compiler provides default no-argument constructor, when we explicitly define parameterized constructor in a class ?
- Compiler won’t provide/inserts default no-arg constructor during compilation process, if programmer defines constructor explicitly (whether it is either default constructor or parameterized constructor)
Q) What happens, when there is 1 or more parameterized constructor defined explicitly ?
- Compilation succeeds
- But, if we try to create new object using default constructor then compiler throws error
Q) If we define constructor in a class, then will it have default constructor ?
- No, there won’t be any default constructor
- Programmer needs to define no-arg constructor explicitly, if required
Q) Can we have both default constructor and parameterized constructor in a class ?
- Yes, constructor overloading is possible
- Both default constructor and 1 or more parameterized constructor can co-exists in a class
- Read constructor overloading concept in detail
3. Constructor on return type:
Q) Why return type is not allowed for constructor ?
- When we add return type to constructor, then compiler treats it 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 it as method with method-name being 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 initializing values
4. 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 class
- 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 an object, if we make constructor as private ?
- Add private modifier to constructor which becomes private constructor
- By making constructor as private makes difficult for other classes to instantiate object of this class
- Read private constructor in detail on how to instantiate objects of this class and access methods
Q) Can we declare constructor as ‘protected’ ?
- Yes, we can declare constructor with ‘protected’ as modifier
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’ constructor is marked as protected
5. 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
6. 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
7. 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
8. 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
9. 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 of same class, then we can 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’ 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 NOT possible
- Using super() constructor call, we can invoke super-class’ constructor from sub-class constructor but its 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
10. 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 (inheritance concept)
Q) Can an Interface have constructor in Java ?
- Defining constructors inside Interface is not allowed and compilation error 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
Related Articles:
- Java Constructor with example
- Default constructor
- Parametrized constructor
- Default constructor v/s Parametrized constructor
- Constructor overloading
- Constructor chaining
- Constructor v/s Methods
- Private Constructor
- Static constructor in Java – A big interview question ?
References:
Happy Coding !!
Happy Learning !!