Q1. Is Constructor Definition Is Mandatory In Class?
No, it is optional . If we do not define a constructor compiler will define a default constructor.
Q2. 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.
Q3. If We Place Return Type In Constructor Prototype Will It Leads To Error?
No, because compiler and JVM considers it as a method.
Q4. When Compiler Provides Default Constructor?
Only if there is no explicit constructor defined by developer.
Q5. 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.
Q6. What Is Default Accessibility Modifier Of Default Constructor?
It is assigned from its class.
Q7. 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.
Q8. 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.
Q9. Can We Declare Constructor As Private?
- Yes we can declare constructor as private.
- All four access modifiers are allowed to constructor.
- We should declare constructor as private for not to allow user to create object from outside of our class.
- Basically we will declare private constructor in Singleton design pattern.
- Read more at Can we create private constructor in java
Q10. 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.
Q11. Why Compiler Given Constructor Is Called As Default Constructor?
Because it obtain all its default properties from its class.
- Its accessibility modifier is same as its class accessibility modifier
- Its name is same as class name.
- Its does not have parameters and logic.
Q12. 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.
Q13. 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.
Q14. 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.
Q15. If Class Has Explicit Constructor , Will It Has Default Constructor?
No. compiler places default constructor only if there is no explicit constructor.