Page 280 - PowerPoint Presentation
P. 280

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

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

                        Animal animal;        // creation of an object Animal
                        animal = new Dog();
                        animal.eat();
                        animal = new Cat();
                        animal.eat();
                        animal = new Lion();
                        animal.eat();
                  }    }

                    // Java Runtime Polymorphism with Multilevel Inheritance
                    class Animal {
                        void eat() {
                               System.out.println(“Animal is eating…”);
                        }
                  }
                        class Dog extends Animal {

                               void eat() {
                                       System.out.println(“Dog is eating fruits…”);
                               }
                        }
                        class BabyDog extends Dog {
                               void eat() {
                                       System.out.println(“Baby Dog is drinking milk…”);
                               }
                        }
                  public class TestPolymorphism4 {
                        public static void main(String[]args) {

                        Animal a1, a2, a3;

                        a1 = new Animal();
                        a2 = new Dog();

                        a3 = new BabyDog();
                        a1.eat();
                        a2.eat();
                        a3.eat();
                  }
                  }

               QUIZ  NO  6.  Hardcoding:  Write  a  program  on  a  piece  of  paper  that  applies  ‘Multi-level
               Inheritance’.
               Intructions:
                   1.  Write a program that has 3 classes besides the main class namely Student, Marks and
                       Results.
                   2.  On Student class, add a method that allows the user to ENTER a Student ID and
                       Student Name. use method name insertStudentInfo()
                   3.  On Student class also, add another method that displays the Information entered by
                       the user on method insertStudentInfo(). Use method name displayInfo()
                   4.  On Marks Class, add a method that allows the user to enter a grade for Math, English
                       and Filipino subject. use method name insertMarks()



                                                                                                            56
   275   276   277   278   279   280   281   282   283   284   285