Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private constructor in abstract class

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?

like image 632
Chris Avatar asked Jul 24 '12 06:07

Chris


People also ask

What is use of private constructor in abstract class C#?

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.

Can constructor be private in a class?

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.

How do you call a constructor from an abstract class?

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.

Why there is constructor in abstract class?

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.


3 Answers

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, enums 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.

like image 50
Marko Topolnik Avatar answered Oct 15 '22 17:10

Marko Topolnik


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.

like image 21
Thilo Avatar answered Oct 15 '22 19:10

Thilo


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.

like image 35
Jeshurun Avatar answered Oct 15 '22 18:10

Jeshurun