Search code examples
javainheritancestatic-factory

Can I Create a Subclass Instance Utilising a Factory-like Static Method of the Superclass?


Problem:

I want to extend Java's java.util.BitSet, using my own MyBitSet, just to add some functionality/transformation methods I often use.

A method of BitSet I really find useful in my code is the "factory-like" public static method BitSet.valueOf(long[]), so I want my MyBitSet class to offer the same utility of creating a MyBitSet instance from a provided long[].

Problem is that Java's BitSet provides no public constructor accepting a long[] argument so I can't just use super().

And BitSet.valueOf(long[]) returns a new BitSet object, so I can't just cast it to my MyBitSet subclass due to a ClassCastException.

Also, the BitSet.valueOf(long[]) method sets some of BitSet's private variables to which I don't have access through my MyBitSet subclass, so I don't see any easy way of just copying the superclass's implementation directly -a bad solution, but I'm just considering my options here.

Question:

Can I somehow provide such a functionality in my MyBitSet subclass?

In general, is it possible to utilise "factory-like" static methods, like valueOf(long[]), to create an instance of my own subclass?

Is there anything I might have overlooked?

Solution I would rather avoid:

An alternative solution would be to use a wrapper class of BitSet instead of extending it.

Namely, a class holding a BitSet instance as a variable and applying the additional functionalities I require in that BitSet variable.
However this approach feels a little weird right now and I would rather just extending BitSet if possible.

Food-for-thought type of question:

BitSet.valueOf(long[]) is a method that creates a new BitSet instance in a specific, initialised state, instead of a default state.

Could it be that offering no such public constructor, but only a "factory-like" static method instead, is actually an intended pattern, so as to prevent developers from using it to initialise their own subclass instance in a state other than the default?

If so, why? What would the problem be in such a case?


Solution

  • Can I somehow provide such a functionality in my MyBitSet subclass?

    In general, is it possible to utilise "factory-like" static methods, like valueOf(long[]), to create an instance of my own subclass?

    You can of course add a new static factory method in your subclass; but it will not inherit any method from the parent class. Clients will have to find your new static method and know to call it instead.

    Is there anything I might have overlooked?

    You say that a wrapper class feels a little weird. I don't know what that means, but you may have heard, Prefer composition over inheritance.

    a "factory-like" static method instead, is actually an intended pattern

    Yes, Static Factory Method is most certainly an intended pattern. Its pros and cons are listed in the tag wiki. The pattern can be fairly flexible or totally inflexible depending on how it is implemented. If the author wants to use it to enforce specific defaults, that is one inflexible use case.