File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes purpose of private constructor in abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "purpose of private constructor in abstract class" Watch "purpose of private constructor in abstract class" New topic

purpose of private constructor in abstract class

Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
Can anyone tell me is their a practical use for having a private constructor in an abstract class??

Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If I am mistaken about this, someone please correct me, but here is my understanding.

If there are multiple constructors defined within the abstract class, you could define some of those constructors to be private to ensure that they are only accessed from within the class itself. Perhaps this is some code that is common to all constructors, but you don't want it to be directly accessible from outside classes - you'd rather invoke that/those constructors from your other, more accessible, constructors.

However, if you only define private constructors, you're going to get a compiler error. The abstract class itself will compile without problem (I think), but any class that tries to extend that class will end up with an error. The reason for this is that the subclass needs to invoke a constructor of the parent class in order to complete the instantiation procedure. If the parent class has ONLY private constructors, you'll get an error here. This goes for ALL classes, not just abstract ones. A class with only private constructors defined is, in effect, implicitly final.

SCJP Tipline, etc.
Swamy Nathan
Ranch Hand

Joined: May 16, 2004
Posts: 187
I dont think there is any scope for correction.

I guess we all also know that if a class is a util class like java.lang.Math which has only static methods to prevent annyone from unnecessarily instantiating such classes one can make their no arg constructor private.[Note this is not about abstract class.]

If u make a class A abstract it implies you will be extending that class A. Otherwise the abstract class A would be useless. And if you extend the abstract class A to derived class B and A had only private constructors how will any constructor of B ever invoke super()? Therefore....
[ June 17, 2004: Message edited by: Swamy Nathan ]
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
makes sense having non-private constructor along with private constructor.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
For example, if you only ever want a constructor with certain arguments to be used, you could make the no-argument constructor private. This would ensure that the initialisation in the constructor with arguments is always performed and you won't have to rely on the goodwill of people extending your class.

I agree. Here's the link:
subject: purpose of private constructor in abstract class
It's not a secret anymore!