Constructor Interview Questions in Java
Interview Questions on Constructor in Java
Why use constructor ?
The main purpose of create a constructor is, for placing user defined values in place of default values.
Why constructor not return any value ?
Constructor will never return any value even void, because the basic aim constructor is to place value in the object
Why constructor definition should not be static ?
Constructor definition should not be static because constructor will be called each and every time when object is created. If you made constructor is static then the constructor before object creation same like main method.
Why constructor is not inherited ?
Constructor will not be inherited from one class to another class because every class constructor is created for initialize its own data members.
What is purpose of default constructor ?
The purpose of default constructor is to create multiple object with respect to same class for placing same value.
What is purpose of parameterized constructor ?
The purpose of parametrized constructor is to create multiple object with respect to same class for placing different value of same type or different type or both.
Is constructor inherited?
No, constructor is not inherited.
Can you make a constructor final?
No, constructor can't be final.
What is the purpose of default constructor?
The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.
Can we use both "this" and "super" in a constructor ?
No, because both this and super should be the first statement.
Does constructor return any value?
yes, that is current instance (You cannot use return type yet it returns a value).
What is flow of constructor in Java ?
Constructor are calling from bottom to top and executing from top to bottom.
Why overriding is not possible at constructor level. ?
The scope of constructor is within the class so that it is not possible to achieved overriding at constructor level.
Difference between Method and Constructor
|1||Method can be any user defined name||Constructor must be class name|
|2||Method should have return type||It should not have any return type (even void)|
|3||Method should be called explicitly either with object reference or class reference||It will be called automatically whenever object is created|
|1||Method is not provided by compiler in any case.||The java compiler provides a default constructor if we do not have any constructor.|