Constructors can be overloaded as any other method as well and I am aware of that fact. Due to a task I decided to use an abstract superclass with multiple constructors:
Abstract Superclass:
protected ListSortierer()
{
this( null, null );
}
protected ListSortierer( List<E> li )
{
this( li, null );
}
protected ListSortierer( Comparator<E> comp )
{
this( null, comp );
}
protected ListSortierer( List<E> li, Comparator<E> com )
{
this.original = Optional.ofNullable( li );
this.comp = Optional.ofNullable( com );
}
To access each of these constructors I needed multiple constructors in the subclass as well.
BubbleSort.java:
public ListBubbleSort()
{
super();
}
public ListBubbleSort( List<E> li )
{
super( li );
}
public ListBubbleSort( Comparator<E> com )
{
super( com );
}
public ListBubbleSort( List<E> li, Comparator<E> com )
{
super( li, com );
}
In this case every constructor of the subclass calls the constructor of the superclass immediately.It came to my mind that I could refer to the own constructor again and pass null
values:
public ListBubbleSort()
{
this( null, null );
}
public ListBubbleSort( List<E> li )
{
this( li, null );
}
public ListBubbleSort( Comparator<E> com )
{
this( null, com );
}
public ListBubbleSort( List<E> li, Comparator<E> com )
{
super( li, com );
}
Doing so would allow me to omit 3 of the overloaded constructors in the abstract superclass but would enforce that every subclass follows the same pattern.
My question is: What is the the better approach in case of consistency?Handle missing values in the abstract superclass or in the subclass? Does it make a difference regarding instantiation or is it just a matter of opinion?
Overloaded constructors essentially have the same name (exact name of the class) and differ by number and type of arguments. A constructor is called depending upon the number and type of arguments passed. While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.
Yes! Java supports constructor overloading. In constructor loading, we create multiple constructors with the same name but with different parameters types or with different no of parameters.
The constructor overloading enables the accomplishment of static polymorphism. The class instances can be initialized in several ways with the use of constructor overloading. It facilitates the process of defining multiple constructors in a class with unique signatures.
Static constructors cannot be inherited or overloaded. A static constructor cannot be called directly and is only meant to be called by the common language runtime (CLR). It is invoked automatically. The user has no control on when the static constructor is executed in the program.
What is a better approach in case of consistency?
Introduce static factory methods.
ListBubbleSort.withList(List<E> list)
ListBubbleSort.withComparator(Comparator<E> comparator)
Make a call to a proper super
constructor. Don't pass any null
s.
public static <E> ListBubbleSort withList(List<E> list) {
return new ListBubbleSort(list);
}
private ListBubbleSort(List<E>) {
super(list);
}
protected ListSortierer(List<E>) {
// initialise only the list field
this.origin = list;
}
Don't use Optional
as a field.
this.original = Optional.ofNullable(li);
Consider the Builder Pattern if you have 3+ parameters.
Handle missing values in the abstract superclass or in the subclass?
A constructor is supposed to provide initial values. You aren't passing initial values, you are just indicating their absence.
By default, null
is the initial value for reference types. So, there is no need to reassign a field if the value for it hasn't been given.
Does it make a difference regarding instantiation or is it just a matter of opinion?
Readability, maintenance.
I would recommend reading Effective Java by Joshua Bloch:
Creating and Destroying Objects
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With