Page 229 - Computer_Science_F5
P. 229

Computer Science  3.  Imagine you are designing a   Abstraction
                (OOP), how might polymorphism
                                                     Abstraction in  Object-Oriented
                be used with this scenario?
                                                     Programming (OOP) helps you manage
                                                     complexity by focusing on what an object
                simple video game. How would
          FOR ONLINE READING ONLY
                you use classes and objects to
                                                     like driving a car: you only need to know
                represent different elements of the   does, rather than how it does it. Think of it
                game, such as characters, items,     how to drive, not how the engine works.
                and environments? How might          Abstraction lets you use objects as “black
                inheritance and polymorphism         boxes” that do specific tasks, without
                come into play in this scenario?     worrying about their inner workings.
             4.  Consider a superclass “Shape” with
                a method “getArea()”. Subclasses     (a) Abstraction in the real world
                like “Square” and “Circle” inherit   Making coffee with a coffee machine is
                this method.  Which type of          a good example of abstraction.
                polymorphism is at play when the
                appropriate “getArea()” method is    You need to know how to use your
                chosen based on the object’s class   coffee  machine  to  make  coffee.  You
                at runtime?                          need to provide water and coffee beans,
             5.  You    have      a    function      switch it on and select the kind of coffee
                “greetAnimal(animal)” in your        you want to get.
                code. This function can accept
                objects of various animal classes
                (Dog, Cat,  etc.). How  might        The thing you do not need to know is how
                polymorphism be beneficial in this   the coffee machine is working internally
                scenario?                            to brew a fresh cup of delicious coffee.

             6.  Imagine a class hierarchy for       You do not need to know the ideal
                vehicles (Car,  Truck, Bike).        temperature of the water or the amount
                How could polymorphism               of ground coffee you need to use.
                be used to create a function
                “moveAllVehicles(vehicles)” that     The manufacturer of a machine resolved
                works with all vehicle types?        issues of temperature  and quantity, so,
             7.  Polymorphism is often associated    created a coffee machine that now acts as
                with inheritance. Can polymorphism   an abstraction and hides all these details.
                exist without inheritance? Explain   You just interact with a simple interface
                your answer.                         that  does not require  any knowledge
             8.  In the real world, provide an       about the internal implementation. You
                example of something that exhibits   can use the same concept in any Object-
                a similar concept to polymorphism.   Oriented Programming languages.


                                                 220
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   220                                                     23/07/2024   12:33
   224   225   226   227   228   229   230   231   232   233   234