Page 135 - CTS - CSA TP - Volume 2
P. 135
COMPUTER SOFTWARE APPLICATION - CITS
Explanation:
• In this example, we have a superclass Animal and a subclass Dog.
• The superclass Animal has a parameterized constructor that initializes the species attribute.
• The subclass Dog has a parameterized constructor that initializes the name attribute and calls the superclass
constructor using super(species).
• In the main method, we create an instance of Dog with a species of “Canine” and a name of “Buddy”.
• We call methods to display the species and name of the dog.
Constructor chaining allows us to reuse code and avoid redundancy by calling one constructor from another. In
the first example, the constructor chaining within the same class helps in providing multiple ways to initialize object
attributes. In the second example, constructor chaining between superclass and subclass helps in initializing
attributes of both classes efficiently.
120
CITS : IT & ITES - Computer Software Application - Exercise 97