Page 275 - PowerPoint Presentation
P. 275

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

                  class Employee {
                        double salary = 40000.00;

                        }
                  public class Programmer extends Employee {   // by using ‘extends’ keyword, you can inherit
                        int bonus = 1000;                          // the properties of class Employee
                  }
                  Public class TestInheritance {
                  public static void main(String[]args){
                        Programmer Programmer1 = new Programmer();
                        System.out.println(“Programmer’s Salary is: “ + Programmer1.salary);
                        System.out.println(“Programmer’s Bonus is: “ + Programmer1.bonus);
                  } }

               Types of Inheritance in Java
                       On the basis of class, there can be three types of inheritance in java: single, multilevel
               and hierarchical.
                                                          Class_A
                          Class_A                                                       Class_A
                                                                        3.  Hierarchical
                 1.  Single                2.  Multilevel
                          Class_B                         Class_B
                                                                                Class_B          Class_C

                                                          Class_C


                       In Java Programming, multiple and hybrid inheritance is supported through interface
               only. We will learn about interfaces later.
               Note: Multiple inheritance is not supported in Java through classes.

               When one class inherits multiple classes, it is known as multiple inheritance.

                          Class_B          Class_C                            Class_A
                                                               5.  Hybrid
                   4.  Multiple
                                                                      Class_B         Class_C
                                  Class_A


                                                                              Class_A
               Single Inheritance Example
                  class Animal {                                       public class TestInheritance2 {

                        void eat() {                                   public static void main(String[]args) {
                              System.out.prntln(“The animal is eating…”);
                        }                                                     Dog D = new Dog();
                  }
                  class Dog extends Animal {                                  D.eat();
                        void bark() {                                         D.bark();
                              System.out.println(“The dog is barking…”);   }
                        }                                              }
                  }





                                                                                                            51
   270   271   272   273   274   275   276   277   278   279   280