Page 226 - Computer_Science_F5
P. 226

drawbacks of using multilevel       two cars, but consider them subclasses
                   inheritance in an OOP application.  of  a  superclass  that  defines  these
               3.  Provide a simple, real-world        operations. Also, if you imagine a super
                   example of multilevel inheritance   class named “Animal.” As explained in       Chapter Four: Object oriented programming with C++
                   involving a base class, a derived   the  previous  paragraph  in inheritance,
          FOR ONLINE READING ONLY
                   class, and a further derived class,   this class might have a method called
                   and explain how methods and         “makeSound()”.  Coming to subclasses
                   attributes are inherited and extended   like  “Dog” and “Cat”  that  inherit
                   at each level?                      properties from “Animal.”, both “Dog”
                                                       and “Cat”  inherit  the  “makeSound()”
                                                       method.  This  reflects  Polymorphism
              Polymorphism                             especially when “makeSound()” behave
              Polymorphism is an important concept     differently  for Dog class and for Cat
              in  Object-Oriented  Programming that    class. The  “makeSound()”  on a  “Dog”
              allows objects from various classes      object when it is called, it might bark,
              to be  viewed  as members  of the        while calling “makeSound()” method on
              same superclass. It allows a single      a “Cat” object might produce a meow.
              interface  to  represent  various  sorts of
              objects,  increasing  flexibility  and  code   (a) Roles of polymorphism
              reusability.  Polymorphism enables  a     (i)  Code reusability: Polymorphism
              same method name to perform different          makes code reuse possible by giving
              actions depending on the type of object        classes the ability to implement a
              it is called on.                               common interface. In other words, a
                                                             single piece of code can interact with
              Example of polymorphism in real world is       multiple objects, which eliminates
              when you consider parents and children.        duplication and encourages effective
              The human body can be viewed as an             code reuse.
              object that has properties and methods.
              Examples  include  private  properties     (ii)  Flexibility and adaptability: Code
              like the brain, heart, liver, stomach and      can be written in a more flexible
              lungs, as well as public properties like       and adaptive manner thanks to
              the  eyes,  ears,  fingers,  head,  hair  and   polymorphism. Code can handle
              legs.  Another simple example is when          several object types without knowing
              you  reflect  someone  who  understands        their unique class by programming
              how to drive can get into and drive            to an interface instead of specific
              most cars since  they  have  key traits        implementations. As a result, the
              such as a steering wheel, a gear lever,        code is more adaptable and able to
              and clutch, brake, and accelerator pedals      handle a larger variety of situations.
              that  the  driver  is  familiar  with.  There     (iii)  Simplifies  code  design:
              may be many differences between any            Polymorphism makes code design


                                                    217
               Student’s Book  Form Five



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