I know that constructors cannot be overridden and hence there is no point in declaring constructor as final. However is there any other technical reason behind this restriction ?
read section 8.2
The members of a class type are all of the following:
Members inherited from its direct superclass (�8.1.3), except in class Object, which has no direct superclass
Members inherited from any direct superinterfaces (�8.1.4)
Members declared in the body of the class (�8.1.5)
Members of a class that are declared private are not inherited by subclasses of that class. Only members of a class that are declared protected or public are inherited by subclasses declared in a package other than the one in which the class is declared.
Constructors, static initializers, and instance initializers are not members and therefore are not inherited.
As per the JLS, the section 8.8 Constructor Declarations it says that
Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.
The final modifier comes into picture only when you have the members which are subject to overriding or hiding. Since constructor is NOT in the list, its denied.
Does this help?
Welcome to JavaRanch.
You may need to please adjust your display name according to the JavaRanch naming policy which strictly insists the people to have their first name and last name with a space in between. The name should not be ficticious.
The last name as such in your name "Kr" can be abbreviated. You can do it in Edit Profile.
Could any one let me know why we cannot declare constructors as final ?
You could declare the class as final. This means that the class cannot be sub-classed and therefore no additional constructors could be defined.
A great feature of Java is that a constructor must be called for every class. This is a good thing because it insures that everything gets initialized. There is no way to stop it. A constructor for each class must be called. If you disallow a constructor to be defined in a sub-class, you could not create a subclass, therefore this would be a final class.
Originally posted by Raghavan Muthu:
Hello ABHINAV Kr,
Welcome to JavaRanch.
You may need to please adjust your display name according to the JavaRanch naming policy...
Yes, please do.
For specifics, see the JavaRanch Naming Policy. Note that the first name can be a initials, but the last name cannot. You can edit your display name here. Thank you for your prompt attention!