There are a few topics similar to this, but I couldn't find one with a sufficient answer.
I would like to know what is the best practice for constructor overloading in Java. I already have my own thoughts on the subject, but I'd like to hear more advice.
I'm referring to both constructor overloading in a simple class and constructor overloading while inheriting an already overloaded class (meaning the base class has overloaded constructors).
Thanks :)
If you have a very complex class with a lot of options of which only some combinations are valid, consider using a Builder. Works very well both codewise but also logically.
The Builder is a nested class with methods only designed to set fields, and then the ComplexClass constructor only takes such a Builder as an argument.
Edit: The ComplexClass constructor can ensure that the state in the Builder is valid. This is very hard to do if you just use setters on ComplexClass.
It really depends on the kind of classes as not all classes are created equal.
As general guideline I would suggest 2 options:
While there are no "official guidelines" I follow the principle of KISS and DRY. Make the overloaded constructors as simple as possible, and the simplest way is that they only call this(...). That way you only need to check and handle the parameters once and only once.
From a unit testing standpoint, it'll become easy to test the class since you can put in the resources into it. If the class has many resources (or collaborators as some OO-geeks call it), consider one of these two things:
Make a parameter class
The constructor in Simple only either needs to split the
SimpleParams
parameter:…or make
SimpleParams
an attribute:Make a factory class
Make a factory class that initializes the resources for you, which is favorable if initializing the resources is a bit difficult:
The constructor is then done in the same manner as with the parameter class:
Make a combination of both
Yeah... you can mix and match both ways depending on what is easier for you at the time. Parameter classes and simple factory classes are pretty much the same thing considering the
Simple
class that they're used the same way.I think the best practice is to have single primary constructor to which the overloaded constructors refer to by calling
this()
with the relevant parameter defaults. The reason for this is that it makes it much clearer what is the constructed state of the object is - really you can think of the primary constructor as the only real constructor, the others just delegate to itOne example of this might be
JTable
- the primary constructor takes aTableModel
(plus column and selection models) and the other constructors call this primary constructor.For subclasses where the superclass already has overloaded constructors, I would tend to assume that it is reasonable to treat any of the parent class's constructors as primary and think it is perfectly legitimate not to have a single primary constructor. For example,when extending
Exception
, I often provide 3 constructors, one taking just aString
message, one taking aThrowable
cause and the other taking both. Each of these constructors callssuper
directly.Well, here's an example for overloaded constructors.
In the above example you can see overloaded constructors. Name of the constructors is same but each constructors has different parameters.
Here are some resources which throw more light on constructor overloading in java,
Constructors.
Constructor explanation.