Top Constructor Interview Questions

What is a Constructor?
Constructor is a special method given in OOP language for creating and initializing object.
In java , constructor role is only initializing object , and new keyword role is crating object.

What are the Rules in defining a constructor?
Constructor name should be same as class name.
It should not contain return type.
It should not contain Non Access Modifiers: final ,static, abstract, synchronized
In it logic return statement with value is not allowed.
It can have all four accessibility modifiers: private , public, protected, default
It can have parameters
It can have throws clause: we can throw exception from constructor.
It can have logic, as part of logic it can have all java legal statement except return statement with value.
We can not place return in constructor.

If we place return type in constructor prototype will it leads to Error?
No, because compiler and JVM considers it as a method.

Can we define a method with same name of class?
Yes, it is allowed to define a method with same class name. No compile time error and no runtime error is raised, but it is not recommended as per coding standards.

How compiler and JVM can differentiate constructor and method definitions of both have same class name?
By using return type , if there is a return type it is considered as a method else it is considered as constructor.

How compiler and JVM can differentiate constructor and method invocations of both have same class name?
By using new keyword, if new keyword is used in calling then constructor is executed else method is executed.

Why return type is not allowed for constructor?
As there is a possibility to define a method with same class name , return type is not allowed to constructor to differentiate constructor block from method block.

Why constructor name is same as class name?
Every class object is created using the same new keyword , so it must have information about the class to which it must create object .
For this reason constructor name should be same as class name.

Can we declare constructor as private?
Yes we can declare constructor as private.
All four access modifiers are allowed to constructor.
Private constructors are used to restrict the instantiation of a class. When a class needs to prevent other classes from creating it’s objects then private constructors are suitable for that. Objects to the class which has only private constructors can be created within the class. Singleton design pattern uses a private constructor. This ensures only one instance of a class exist at any point of time. Here is an example of singleton pattern using private constructor.

 

 

Is Constructor definition is mandatory in class?
No, it is optional . If we do not define a constructor compiler will define a default constructor.

Why compiler given constructor is called as default constructor?
Because it obtain all its default properties from its class.
They are
1.Its accessibility modifier is same as its class accessibility modifier
2.Its name is same as class name.
3.Its does not have parameters and logic.

what is default accessibility modifier of default constructor?
It is assigned from its class.

When compiler provides default constructor?
Only if there is no explicit constructor defined by developer.

When developer must provide constructor explicitly?
If we want do execute some logic at the time of object creation, that logic may be object initialization logic or some other useful logic.

If class has explicit constructor , will it has default constructor?
No. compiler places default constructor only if there is no explicit constructor.

What are the ways object can be created in java?
Using newInstance() Method
using Class.forName(“packageName.MyClass”);
Using clone() method.
Using object deserialization

What is constructor chaining?
Constructor Chaining is a technique of calling another constructor from one constructor. this() is used to call same class constructor where as super() is used to call super class constructor.

 

Can we call sub class constructor from super class constructor?
No. There is no way in java to call sub class constructor from a super class constructor.

What happens if you keep return type for a constructor?
JVM treat it as a normal method. But compiler gives a warning saying that method has a constructor name.

What is No-arg constructor?
A Constructor without any arguments is called no-arg constructor. Default constructor in java is always a no-arg constructor.
In case if Developer does not specify any constructor in a class then JVM provides a default constructor which is a no-arg constructor.

Can we use  this() and super() in a method?
No, We can’t use this() and super() in a method.

 

 

   

Comments are closed