Page 361 - CITS - Computer Software Application -TT
P. 361

COMPUTER SOFTWARE APPLICATION - CITS



           File: TestInheritance.java

              class Animal{
              void eat(){System.out.println(“eating...”);}
              }
              class Dog extends Animal{
              void bark(){System.out.println(“barking...”);}
              }

              class TestInheritance{
              public static void main(String args[]){
              Dog d=new Dog();
              d.bark();

              d.eat();
              }}
           Output:
           barking...
           eating...

           Multilevel Inheritance Example
           When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given
           below, BabyDog  class inherits  the Dog class which  again inherits  the Animal  class, so there is a multilevel
           inheritance.
           File: TestInheritance2.java
              class Animal{
              void eat(){System.out.println(“eating...”);}

              }
              class Dog extends Animal{
              void bark(){System.out.println(“barking...”);}
              }

              class BabyDog extends Dog{
              void weep(){System.out.println(“weeping...”);}
              }
              class TestInheritance2{
              public static void main(String args[]){
              BabyDog d=new BabyDog();
              d.weep();
              d.bark();
              d.eat();
              }}
           Output:
           weeping...

           barking...
           eating...


                                                           348

                             CITS : IT&ITES - Computer Software Application - Lesson 94 - 100
   356   357   358   359   360   361   362   363   364   365   366