Page 225 - Computer_Science_F5
P. 225

Computer Science  parent  class. In Figure 4.4,  Class A  is  classes B and C inheriting  from the
           shown as the parent class, while Class B  parent class A.
           is portrayed as the child class inheriting
                                                                     Class A
           properties and behavior from its parent
           class.
          FOR ONLINE READING ONLY
                           Class A                        Class B               Class C
                                                         Figure 4.6: Hierarchical inheritance

                           Class B
                                                     (iv) Hybrid inheritance
                  Figure 4.4: Single inheritance     Hybrid inheritance is formed by
                                                     combining  multiple  inheritance  and
           (ii) Multilevel inheritance               multilevel  inheritance,  typically  using
           Multilevel inheritance happens when a  interfaces due to Java’s lack of support
           class inherits from another class, which in  for multiple inheritance. In Figure 4.7,
           turn inherits from another class, forming a  Class A acts as the parent class for both
           chain of inheritance. In Figure 4.5, Class C  classes  B and  C. Classes B  and  C, in
           inherits from Class B, and Class B inherits  turn, serve as parent classes for class D,
           from Class A. This means that Class C  which is the sole child class of both B
           inherits both the traits of Class B and  and C.
           the properties and behavior of Class A.
           Thus, Class C automatically acquires the                  Class A
           properties and methods of both Class A
           and Class B.                                   Class B               Class C


                           Class A
                                                                     Class D


                           Class B                       Figure 4.7: Hierarchical inheritance


                                                          Exercise 4.3
                           Class C                    1.  Analyse the statement that,
                                                         “Inheritance is the process by
                 Figure 4.5: Multilevel inheritance
                                                         which objects of one class acquire
                                                         the properties of objects of another
           (iii) Hierarchical inheritance                class.”
           A class is considered to have hierarchical
           inheritance when it serves as the parent   2.  How does multilevel inheritance
           class for multiple  child classes or          differ from single inheritance?
           subclasses. Figure 4.6, shows the child       Explain the potential benefits and

                                                 216
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   216                                                     23/07/2024   12:33
   220   221   222   223   224   225   226   227   228   229   230