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.
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 ]
Joined: Jun 02, 2003
makes sense having non-private constructor along with private constructor. thanks
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.
subject: purpose of private constructor in abstract class