7

I got a design question in my mind. Suppose if I have a class which has only static methods, what would be the best design option from the following two.

  • Make the class abstract
  • Make the constructor private, so that no code outside the class can make an object of this class.

Does the choice depend on the situation or there is one best way to go? And why?

DesirePRG
  • 6,122
  • 15
  • 69
  • 114

5 Answers5

9

Making a class abstract assumes that you want this class to be inherited from. If you want this to happen, then make it abstract.

If you only have static Methods (so it's some kind of utility class) then go with the second way.
Though there is nothing wrong in creating an Instance of this class, as there is no benefit or disadvantage that way, the best practice is to make the constructor private for utility classes.

Loki
  • 4,065
  • 4
  • 29
  • 51
4

Let's look at what the developers of standard classes did :

public class Arrays {
    // Suppresses default constructor, ensuring non-instantiability.
    private Arrays() {
    }

public class Collections {
    // Suppresses default constructor, ensuring non-instantiability.
    private Collections() {
    }

I see a pattern here.

This makes sense, since an abstract class implies that the class should be sub-classed, which is not the case when your class is a utility class having only static methods.

Eran
  • 387,369
  • 54
  • 702
  • 768
4

I think the better approach is to create final class with a private constructor. Because The purpose of an abstract class is to function as a base for subclasses.

Rahul Thachilath
  • 363
  • 3
  • 16
  • but if i make it final, I wont be able to extend the class in the future, if I ever wanted to. so is that a good way to go? – DesirePRG Apr 13 '15 at 07:28
  • 1
    if you are making the constructor private it by default make your class unextendable. You have also mentioned that the class will only contain static methods. so the extending the class is also meaning less :) – Rahul Thachilath Apr 13 '15 at 07:30
  • 1
    http://www.oodesign.com/ nice place to learn some design priciples.. :) happy coding. – Rahul Thachilath Apr 13 '15 at 07:33
2

Private constructor for sure. In general, a class with only static methods should be:

public final class Utility { 
    public static void foo() { }
    // ... etc.
}

If you declared it abstract, it's reasonable to assume you intended it to be inherited, which is not the case from your description.

The final declaration ensures that it cannot be extended.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
  • does making it final really necessary? what if i want to extend it in the future? – DesirePRG Apr 13 '15 at 07:29
  • Then you wouldn't declare it final. I'll readily admit I've never encountered a scenario where I wanted to extend a utility class though. – jdphenix Apr 13 '15 at 07:32
  • A comment states that making the constructor private, stops me from extending the class. so does making it final give me any additional value? – DesirePRG Apr 13 '15 at 07:37
  • @DesirePRG Take a look over this question and it's top answer, http://stackoverflow.com/questions/218744/good-reasons-to-prohibit-inheritance-in-java – jdphenix Apr 13 '15 at 07:41
1

I'd say "both".

Making the class abstract prevents potential user from creating its instance even using reflection. It guarantees that user pays attention that this is pure utility class and should not be instantiated.

Constructor of abstract class should never be public. If class is dedicated for extension the constructor should be protected because it anyway can be used by subclasses only. But your class cannot be inherited. Therefore its constructor can be only private. Moreover, to be on the safe side constructor can throw IllegalStateException. In this case even if somebody in future makes it public he cannot call it without changing its code.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • what you are saying is I need an abstract class with a private constructor? – DesirePRG Apr 13 '15 at 10:06
  • @DesirePRG, IMHO yes. However take a look on other answers as well. Other people think that you should define class `abstract` only if you *want* to extend it. – AlexR Apr 13 '15 at 12:09
  • Declaring constructors `private` only disables inheritance from the outside... Inside the class, you may still inherit it with a named or an anonymous derived class. – Yousha Aleayoub Sep 04 '18 at 12:58