This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Final Class and Class with private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Final Class and Class with private constructor" Watch "Final Class and Class with private constructor" New topic
Author

Final Class and Class with private constructor

Arivazhagan Arutchelvam
Greenhorn

Joined: Feb 19, 2008
Posts: 10
Is it possible to extend a class which is having only a private constructor?

If so does it means the same to have a final class and a class with the private constructor?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Originally posted by Arivazhagan Arutchelvam:
Is it possible to extend a class which is having only a private constructor?

No, because the subclass needs access to at least one constructor of the superclass.
If so does it means the same to have a final class and a class with the private constructor?

Making a class final also makes it impossible to subclass, so this has the same effect in this regard as making all constructors of the superclass private.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Arivazhagan Arutchelvam
Greenhorn

Joined: Feb 19, 2008
Posts: 10
Thanks Jesper Young.

Is there any difference between the Final Class and a Class with a private constructor in any other context?
[ February 20, 2008: Message edited by: Arivazhagan Arutchelvam ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you use nested classes, it's possible to subclass a class with a final constructor using another class contained within the same top-level class. Private does not prevent this sort of access.

Another difference is that if you have a private constructor, it's always possible to add another non-private constructor as well, and then the class can be subclassed.

Most importantly, if you want to prevent a class from being subclassed, then "final" makes this fact very clear. Using a private constructor may have a similar effect, but it will confuse the reader trying to figure out what you're up to.


"I'm not back." - Bill Harding, Twister
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It may still be possible to instantiate the final class
Arivazhagan Arutchelvam
Greenhorn

Joined: Feb 19, 2008
Posts: 10
So it concludes that, it is not possible to create instance for a class having only private constructors, anywhere else in the same class itself
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You can instantiate a class with private constructors by using Reflection. This will work unless SecurityManager disallows it.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Also you can instantiate a method with private constructors if the class provides a static factory method. Look at java.util.regex.Pattern for example. Of course this has nothing to do with the original question about subclassing and final.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Final Class and Class with private constructor
 
Similar Threads
Math Class is Final?
Constructor in private
Constructors and Access Modifiers
Difference between private constructor and making class final ?
A constructor cannot be final, static or abstract. Why ?