Constructor Interview Questions in Java

Advertisements

Prev Tutorial Next Tutorial

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

MethodConstructor
1Method can be any user defined nameConstructor must be class name
2Method should have return typeIt should not have any return type (even void)
3Method should be called explicitly either with object reference or class referenceIt will be called automatically whenever object is created
1Method is not provided by compiler in any case.The java compiler provides a default constructor if we do not have any constructor.

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements