can we make a constructor final in java?

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

Please follow and like us:
Content Protection by DMCA.com