Page 211 - Scholarly Works of Faculty
P. 211

CAVITE STATE UNIVERSITY
                               T3 CAMPUS
                               Department of Information Technology          COSC 65 – Programming Languages


                                       Employee                           Programmer
                                     salary : double                       bonus : int

                       As displayed in the above figure, Programmer is the subclass and Employee is the
               superclass.  The  relationship  between  the  two  classes  is  Programmer  IS-A  Employee.  It
               means that Programmer is a type of Employee.

                  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








                                                                                                 Page | 70
   206   207   208   209   210   211   212   213   214   215   216