Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Final Class and Class with private constructor

 
Arivazhagan Arutchelvam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15635
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arivazhagan Arutchelvam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may still be possible to instantiate the final class
 
Arivazhagan Arutchelvam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can instantiate a class with private constructors by using Reflection. This will work unless SecurityManager disallows it.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic