Private constructor in abstract class
Only thing I can think of is reusing common code shared by the other (protected) constructors. They could then call the private constructor in their first line.
If the private
constructor is the only constructor of the class, then the reason is clear: to prevent subclassing. Some classes serve only as holders for static fields/methods and do not want to be either instantiated or subclassed. Note that the abstract
modifier is in this case redundant—with or without it there would be no instantiation possible. As @JB Nizet notes below, the abstract
modifier is also bad practice because it sends wrong signals to the class's clients. The class should in fact have been final
.
There is another use case, quite rare though: you can have an abstract class
with only private
constructors that contains its own subclasses as nested classes. This idiom makes sure those nested classes are the only subclasses. In fact, enum
s in Java use just this idiom.
If there are other constructors around, well then there's really nothing special about the private
constructor. It gets used in an abstract
class just as in any other.