Page 276 - PowerPoint Presentation
P. 276

CAVITE STATE UNIVERSITY
                               TRECE MARTIRES CITY CAMPUS
                               Department of Information Technology            DCIT 111 - Advanced Programming

               Multilevel Inheritance Example
                       class Animal {
                              void eat() {
                                      System.out.prntln(“The animal is eating…”);
                                      }
                       }
                              class Dog extends Animal {
                                      void bark() {
                                      System.out.println(“The dog is barking…”);
                                      }
                       }
                              class BabyDog extends Dog {
                                      void weep(){
                                      System.out.println(“The Baby Dog is weeping”);
                                      }
                       }

                       public class TestInheritance3 {
                              public static void main(String[]args){

                              BabyDog D = new babyDog();

                              D.eat();
                              D.bark();
                              D.weep();
                       }
                       }

               Hierarchical Inheritance Example

                        class Animal {

                               void eat() {
                                      System.out.println(“The animal is eating”);
                                      }
                        }
                               class Dog extends Animal {
                                      void bark() {
                                             System.out.println(“The Dog is barking”);
                                             }
                        }
                               class Cat extends Animal {
                                      void meow() {
                                             System.out.println(“The Cat is Meowing”);
                                             }

                        }
                        public class TestInheritance4 {
                               public static void main(String[]args){

                               Cat c = new Cat();
                               c.meow();
                               c.eat();
                               // c.bark();   will give an error.
                        }
                        }

                                                                                                            52
   271   272   273   274   275   276   277   278   279   280   281