Private constructor in abstract class

Chris :

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?

Marko Topolnik :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

abstract class vs private constructor

From Dev

Private constructor in abstract class Scala?

From Java

making a class abstract vs making the constructor private

From Dev

Fortran constructor of abstract class initializing private variables

From Dev

Can Abstract class have constructor and private member in C++11

From Dev

What is the use of private constructor in abstract class in c#?

From Java

Java abstract class, abstract constructor

From Dev

abstract private inner class

From Dev

Java constructor of an abstract class

From Java

Copy constructor for abstract class

From Dev

Call constructor in an abstract class

From Java

Public constructor of a private class

From Java

extends of the class with private constructor

From Dev

Making class constructor private

From Dev

Testing a class with private constructor

From Dev

subclassing a class with a private constructor

From Java

Invoking Private Methods of an Abstract Class

From Java

Abstract methods in an abstract class constructor: design flaw?

From Dev

Instance of abstract class with hidden constructor

From Java

Constructor injection on abstract class and children

From Dev

Mocking an abstract method, but not the class constructor

From Dev

Java constructor of a subclass of an abstract class

From Java

Abstract class constructor access modifier

From Java

Can an abstract class have a constructor?

From Dev

Call constructor in abstract class with generics

From

Constructor of an abstract class in C#

From Dev

abstract class constructor practical use

From Dev

Benefits of an abstract class with a factory constructor?

From Dev

Problem in copy constructor of a abstract class

Related Related

HotTag

Archive