Page 223 - Computer_Science_F5
P. 223

Computer Science  a method in a class, it is like a method   have access to or can modify
           in an OOP where objects use methods to
                                                         sensitive data. In our example of the
                                                         pet dog house, protection prevents
           access and modify data. In this example,
                                                         someone from directly accessing the
           to protect a dog is like to protect data. I
                                                         dog’s bedroom (data) from outside
           hope you will not like people to reach
          FOR ONLINE READING ONLY
           your dog easily in this house and take or
           kill it. The idea of encapsulation achieves   the house (class). A house as an
                                                         object is also an instance of a class.
           this by making one attribute, like a dog’s   (ii) Controlled modifications: The dog’s
           bedroom, private.  The other method in        door (methods) regulates how the
           this class is a feeding plan method. This     dog interacts with the surrounding
           is the authorised method that can interact    environment. Similarly, methods
           with the dog through other method (dog’s      can enforce rules regarding data
           door). In this example, a dog is like data.   modifications. For example, a
                                                         method for supplying food may only
                                                         allow for a measured quantity to be
                                                         given to the dog.
                                                      (iii)  Information hiding: You do not
                                                         need to be aware of the inner
                                                         workings of the dog door mechanism
                                                         (internal details) in order to use it.
                                                         Encapsulation hides the internal
                                                         implementation details of a class.
                                                         Instead, you interact with the
                    Figure 4.2: Encapsulation            methods (dog’s door) to achieve
                                                         what you intended (feeding the dog).
           In this case, encapsulation in OOP refers   (iv)  Flexibility: Assume you wish to add
           to the practice  of bundling  data  and       an area for scratching to the playroom
           the methods that operate on that data         (an internal change). Encapsulation
           within a single unit, typically a class. It   allows you to change the house’s
           involves hiding the internal details of an    interior (class structure) without
           object and providing a public interface       impacting how people interact with
           to  interact  with  it.  Encapsulating  data   the dog house (object). They still
           and methods together  ensures that  the       utilize the dog door (methods).
           object’s state is accessed and modified
           only through defined methods, known as        Exercise 4.2
           getters and setters.
                                                      1.  How does the concept of
           Importance of encapsulation in OOP            encapsulation help in maintaining
             (i)  Data protection:  Encapsulation        data integrity and promoting
                ensures that only authorised methods     information hiding?

                                                 214
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   214                                                     23/07/2024   12:33
   218   219   220   221   222   223   224   225   226   227   228