0

As we all know constructor cannot be overridden. So when we declare a constructor as final, why does it give compile time error? As final keyword will also let a constructor not to override.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143

2 Answers2

0

Only public,private,protected are allowed as modifiers. final is an not allowed as per the JLS. final in java is to restrict the extension.

In java constructors can not be overridden. Super class's constructor is always called from any Constructor of the base class (implicitly or explicitly). final is used to prevent any method from being overridden, constructors are not like normal methods and also can not be overridden. making constructors as final will not make any sense. Making this will not be defined as any feature.

You can make the class as final if you wish to prevent its extension. If you wish to restrict the constructor of an class just make it private.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
0

No Constructors can NEVER be declared final. Your compiler will always give an error of the type "modifer final not allowed" Final, when applied to methods, means that the method cannot be overridden in a subclass. Constructors are NOT ordinary methods. (different rules apply) Additionally, Constructors are NEVER inherited. So there is NO SENSE in declaring it final.

Ketan G
  • 507
  • 1
  • 5
  • 21