Constructor Interview Question Answer

Define 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 for defining a constructor?
Constructor name should be the same as the class name
It cannot contain any return type
It can have all Access Modifiers are allowed (private , public, protected, default)
It Cannot have any Non Access Modifiers (final ,static, abstract, synchronized)
No return statement is allowed
It can take any number of parameters
Constructor can throw exception, we can have throws clause


Can we declare constructor as private?

 Yes we can declare constructor as private. But it does not  allow user to create object from outside of our class

Comments

Popular posts from this blog

Java tutorial by Balakrishna Reddy