File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What good is a class if its abstract and has a private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What good is a class if its abstract and has a private constructor" Watch "What good is a class if its abstract and has a private constructor" New topic
Author

What good is a class if its abstract and has a private constructor

Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
I was just thinking if there is any benefit of having an abstract class and then make it's constructor private. Is there any user of such classes?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
First use is for classes that are not instantiable and are really just collections of static utility methods. A well-designed Java program won't have a lot of such classes (they aren't very OO), but occasionally they are useful.

A second use might be a class that has a static factory method returning instances of subclasses of itself. The subclasses would have to be nested inside the main class, or the private constructor would be inaccessible. A need for such classes won't come up often.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Peter Chase:

A second use might be a class that has a static factory method returning instances of subclasses of itself. The subclasses would have to be nested inside the main class, or the private constructor would be inaccessible. A need for such classes won't come up often.


Before Java 5, it was a well-known idiom for implementing typesafe enums.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Lucas Lee
Ranch Hand

Joined: Oct 02, 2006
Posts: 53
I think the most famous usage of private constructor is to implmenting a singleton .
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Lucas Lee:
I think the most famous usage of private constructor is to implmenting a singleton .


Yes, but that is generally not in combination with an abstract class, which is the specific topic we're discussing here.
Lucas Lee
Ranch Hand

Joined: Oct 02, 2006
Posts: 53
Originally posted by Peter Chase:


Yes, but that is generally not in combination with an abstract class, which is the specific topic we're discussing here.


Yes, you're right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What good is a class if its abstract and has a private constructor
 
Similar Threads
class Math is concrete class but still it cannot be instantiated.
abstract class
access modifier for an abstract class
final and abstract
Significance of constructors in abstract class