Saturday, January 4, 2020
Implicit and Explicit Constructor Chaining
Constructor chaining in Java is simply the act of one constructor calling another constructor via inheritance. This happens implicitly when a subclass is constructed: its first task is to call its parents constructor method. But programmers can also call another constructor explicitly using the keywordsà this() orà super(). The this() keyword calls another overloaded constructorà in the same class; the super() keyword calls a non-default constructor in a superclass. Implicit Constructor Chaining Constructor chaining occurs through the use of inheritance. A subclass constructor methods first task is to call its superclass constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain. There could be any number of classes in an inheritance chain. Every constructor method calls up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining. Note that: This implicit call to the superclass is the same as if the subclass had included the super() keyword, i.e. super() is implicit here.If a no-args constructor is not included in the class, Java creates one behind the scenes and invokes it. This means that if your only constructor takes an argument, you must explicitly use a this() or super() keyword to invoke it (see below). Consider this superclass Animal extended by Mammal: class Animal {// constructorAnimal(){ à System.out.println(Were in class Animals constructor.);}} class Mammal extends Animal {//constructorMammal(){ à System.out.println(Were in class Mammal s constructor.);}} Now, lets instantiate the class Mammal: public class ChainingConstructors { à /*** param args*/public static void main(String[] args) {Mammal m new Mammal();}} When the above program runs, Java implicitly triggers a call to the superclass Animal constructor, then to the class constructor. The output, therefore, will be: Were in class Animals constructorWere in class Mammals constructor Explicit Constructor Chaining using this() or super() Explicit use of the this() or super() keywords allows you to call a non-default constructor. To call a non-args default constructor or an overloaded constructor from within the same class, use theà this()à keyword.à To call a non-default superclass constructor from a subclass, use the super() keyword. For instance, if the superclass has multiple constructors, a subclass may always want to call a specific constructor, rather than the default. Note that the call to another constructor must be the first statement in the constructor or Java will throw a compilation error. Consider the code below in which a new subclass, Carnivore, inherits from Mammal class which inherits from the Animal class, and each class now has a constructor that takes an argument. Heres the superclass Animal:à public class Animalprivate String name;public Animal(String name)à // constructor with an argument{this.name name;System.out.println(Im executed first.);}}Note that the constructor now takes a name of type String as a parameter and that the body of the class calls this() on the constructor. Without the explicit use of this.name, Java would create a default, no-args constructor and invoke that, instead. Heres the subclass Mammal: public class Mammal extends Animal {public Mammal(String name){super(name);System.out.println(Im executed second);}} Its constructor also takes an argument, and it uses super(name) to invoke a specific constructor in its superclass. Heres another subclass Carnivore. This inherits from Mammal:à public class Carnivore extends Mammal{public Carnivore(String name){super(name);System.out.println(Im executed last);}} When run, these three code blocks would print: Im executed first.Im executed second.Im executed last. To recap: When an instance of the Carnivore class is created, the first action of its constructor method is to call the Mammal constructor method. Likewise, the first action of the Mammal constructor method is to call the Animal constructor method. A chain of constructor method calls ensure that the instance of the Carnivore object has properly initialized all the classes in its inheritance chain.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.