Forums Register Login

Constructor

+Pie Number of slices to send: Send
Could any one let me know why we cannot declare constructors as final ? (Compiler throws error if we try to do that)

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 ?
+Pie Number of slices to send: Send
Have a quick read here

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html

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.
+Pie Number of slices to send: Send
Hi ABHINAV Kr,

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?
+Pie Number of slices to send: Send
Hello ABHINAV Kr,

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.
+Pie Number of slices to send: Send
Welcome to the Java Ranch ABHINAV,

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.

Kaydell
+Pie Number of slices to send: Send
 

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!
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1026 times.
Similar Threads
Constructor
Virtual Constructor
final static variable
Can a Class constructor be Static
Constructor Final ?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:57:45.