No, we cannot make constructor as final in java.
For methods, final keyword is used to prevent them to be overridden by subclass. Constructors are also the special kind of methods but as we know that constructor can not be inherited in subclass, hence there is no use of final keyword with constructor.
If we declare constructor as final, compiler will give compile time error. Please see the below example:
Example
public class Main { final Main(){ System.out.println("Final Constructor"); } public static final void main(String[] args) { Main obj1 = new Main(); } } |
Output
Main.java:11: error: modifier final not allowed here final Main(){ ^ 1 error |
Java interview questions on constructor
- Default constructor
- Does constructor return any value in java?
- Is constructor inherited in java?
- Can you make a constructor final in java?
- Difference between constructor and method in java?
- How to copy values from one object to another java?
- How to overload constructor in java?
- can you create an object without using new operator in java?
- Constructor chaining in java
- Parameterized constructor in java
- Can we call subclass constructor from superclass constructor?
- What happens if you keep return type for a constructor?
- What is the use of private constructor in java?
- Can a constructor call another constructor java?