In Java what is the purpose of using private constructor in an abstract class?
In a review I got this question, and I am curious, for what situation we need to use the constructor in such way?
I think it can be used in pair with another constructor in abstract class, but this is very trivial. Also it can be used for constructing static inner classes which will excend abstract class.
Maybe there is more elegant usage?
Private Constructor is a special instance constructor present in C# language. Basically, private constructors are used in class that contains only static members. The private constructor is always declared by using a private keyword.
Yes, we can declare a constructor as private. If we declare a constructor as private we are not able to create an object of a class. We can use this private constructor in the Singleton Design Pattern.
Conditions for defining a parameterized constructor in an abstract class. We need to make sure that the class which is extending an abstract class have a constructor and it can call the superclass parameterized constructor. We can call the superclass parameterized constructor in a subclass by using super() call.
The main purpose of the constructor is to initialize the newly created object. In abstract class, we have an instance variable, abstract methods, and non-abstract methods. We need to initialize the non-abstract methods and instance variables, therefore abstract classes have a constructor.
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.
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.
Sometimes, the default no-arg constructor is made private, and another constructor which accepts arguments is provided. This constructor might then invoke other private constructor(s) . This forces implementations to supply these arguments, which might ensure some variable is always initialized, although this is not common practice (in my experience). If this is the requirement, you would be better off checking your variables and throwing an IllegalArgumentExeption
, explaining why the variable needs to be initialized.
If you create an abstract class with only private constructors, the class is practically useless as no instances can ever be created. If the intention is to create a utility class with only static methods (like the Math
class in the java.lang
package), private constructors are acceptable, however the class should be marked final instead, as marking the class as abstract implies the class is to be extended.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With