Page 133 - CTS - CSA TP - Volume 2
P. 133

COMPUTER SOFTWARE APPLICATION - CITS





           Output:

































           Explanation:
           •  In this program, we have a class Person with three constructors.
           •  The default constructor initializes the person with default values by calling the parameterized constructor with
              default values.
           •  The parameterized constructor with only the name initializes the person with the provided name and default
              age by calling the parameterized constructor with default age.
           •  The parameterized constructor with both name and age initializes the person with the provided name and age.

           •  The display method prints the details of the person, including name and age.
           •  In the main method, we create instances of Person using different constructors to demonstrate constructor
              chaining.

           •  Finally, we call the display method to print the details of each person.
           This program illustrates how constructor chaining within the same class can be used to provide multiple ways
           of object initialization while avoiding code duplication. It allows for cleaner and more concise code by reusing
           constructor logic.



           TASK 3: Constructor Chaining with Superclass
              class Animal {
                  String species;

                  // Constructor of superclass
                  Animal(String species) {

                      this.species = species;
                  }
                  void displaySpecies() {





                                                           118

                               CITS : IT & ITES - Computer Software Application - Exercise 97
   128   129   130   131   132   133   134   135   136   137   138