File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
ABHINAV KUMAR
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
damien malone
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic