aspose file tools*
The moose likes Beginning Java and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

ABHINAV KUMAR
Greenhorn

Joined: Jun 12, 2007
Posts: 6
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

Joined: May 06, 2003
Posts: 35
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

Joined: Apr 20, 2006
Posts: 3344

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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Nov 18, 2006
Posts: 689

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

Joined: Aug 31, 2004
Posts: 11343

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!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor