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

COMPUTER SOFTWARE APPLICATION - CITS




                      System.out.println(“Species: “ + species);
                  }

              }
              class Dog extends Animal {

                  String name;
                  // Constructor of subclass
                  Dog(String species, String name) {

                      super(species); // Call superclass constructor
                      this.name = name;

                  }
                  void displayName() {
                      System.out.println(“Name: “ + name);

                  }
              }

              public class ConstructorChainingSuperDemo {
                  public static void main(String[] args) {
                      // Creating an instance of Dog

                      Dog dog = new Dog(“Canine”, “Buddy”);
                      // Displaying species and name of the dog

                      dog.displaySpecies();
                      dog.displayName();
                  }

              }


           Output:






























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