Week 3 — What is constructor chaining?
Question of the Week #3
What is constructor chaining?
8 Replies
This means it's possible to have [omitting boilerplate for the sake of readability]
which can be represented as a tree
Constructor chaining refers to creating overloaded constructors of a class, specifically those with "default" parameters, by chaining the constructors linearly.
This allows the caller to omit parameters that are either not important or can assume their default value.
A real world example from the java standard library includes the
BufferedWriter
class
or WeakHashMap
which, ironically unidiomatically, does not use this(DEFAULT_INITIAL_CAPACITY)
but this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR)
which allows us to see that even the standard library is not infallible
While this chaining is linear from any given constructor itself the overall structure is tree-likeIt should be noted though, that performance wise this could have proper reasoning, as each chained call adds a stackframe which can be avoided if you duplicate a tiny bit of code.
While this would not make much of a difference it is possible that this choice was deliberate.
⭐ Submission from jade#9418
As seen here, each constructor calls one with more parameters than itself, until it eventually calls the 'original' constructor which implements the logic.
It is important to notice that each constructor mainly calls another one, and does not implement extra logic. While this is possible it should be avoided unless the computation is pure in itself and is then added as an argument for the call to the overloaded constructor or to a class field.
Constructor chaining is the process of calling a constructor with an object argument that is created using its constructor in the same statement.
If there are two types like the following:
then it is possible to create an instance of
A
and pass that directly to the instance of B
as follows:
As this calls the constructor of A
and passes the created object to B
, it is called constructor chaining.⭐ Submission from dan1st#7327
Constructor chaining is the process of calling a sequence of constructors. We can do it in two ways:
by using this() keyword for chaining constructors in the same class
by using super() keyword for chaining constructors from the parent class
Submission from sahilasopa#2787
In Java, a constructor is a method that is used to initialise an object. Constructor chaining refers to the process of calling one constructor from another constructor, either within the same class or from a subclass.
There are two ways to chain constructors in Java:
Within the same class:
You can call one constructor from another constructor within the same class by using the this keyword. This is useful when you have multiple constructors in a class and you want to share some common initialisation code between them.
For example:
In the above example, the Employee class has three constructors: a default constructor, a constructor that takes a name and age, and a constructor that takes all three parameters (name, age, and department). The default constructor and the constructor with name and age both call the constructor with all three parameters using the this keyword. This allows them to share the common initialization code for setting the name, age, and department fields.
From a subclass:
You can also call a superclass constructor from a subclass constructor using the super keyword. This is useful when you want to inherit the properties and behavior of a superclass, but also add some additional initialization code in the subclass.
For example:
In the above example, the Employee class extends the Person class and adds a department field. The Employee class has a constructor that takes a name, age, and department as parameters. This constructor calls the Person class's constructor using the super keyword, passing in the name and age parameters. This allows the Employee object to inherit the name and age fields from the Person class, and also initialise the department field.
Submission from Liquid#2980
Constructor chaining is the act of calling constructors in the same class (
this()
) or parent class (super()
) during object initialization, from another constructor. A constructor should call super()
first to ensure the inherited members are initialized.
Submission from Fright XO#1337