Page 370 - Computer_Science_F5
P. 370

// The body of animalSound() is provided here  4.  Explain the concept of data
                    System.out.println(“The pig says: wee wee”);  hiding and how it relates to
                  }                                              abstraction in Java. Provide
                }                                                examples to illustrate your
                class Main {                                                                       Chapter Five: Object oriented programming with Java
          FOR ONLINE READING ONLY
                  public static void main(String[] args) {       explanation.
                    Pig myPig = new Pig(); // Create a Pig object  5.  Design a Java program that
                    myPig.animalSound();                         utilizes abstraction to model
                    myPig.sleep();                               a banking system. Include
                  }                                              abstract classes or interfaces
                }                                                to represent different account

               Output:                                           types, such as savings and
                                                                 checking accounts, and
                                                                 demonstrate how abstraction
                                                                 simplifies the implementation
                   Figure 5.28: Output of abstract program       and maintenance of the system.



                  Activity 5.10:                             Encapsulation in Java

                Visualisation of code execution in Java      Encapsulation is a fundamental
                abstraction                                  concept in object-oriented
                                                             programming (OOP) that involves
               Use Jeliot 3 or later version, visualise the   bundling data (attributes) and
               step by step execution of java program to     methods (functions) that operate
               explore about abstraction in Java. Use the    on the data into a single unit,
               code in Program Example 5.27.
                                                             called a class. The significance of
                                                             encapsulation is that it facilitates
                   Exercise 5.5                              data hiding, abstraction, and code
                                                             organisation, using access modifiers,
               1.  Describe how abstraction is implemented   leading to modular and maintainable
                   in Java through abstract classes and      code.
                   interfaces. Provide examples of each.
               2.  Discuss the role of abstract methods in   Access modifiers and use of “this”
                   abstract classes and interfaces. How do   and “final” keywords in Java
                   they contribute to achieving abstraction?  Access modifiers have a significant
               3.  Describe a real-world scenario where      impact on Java programming as
                   abstraction would be beneficial in        they govern the visibility and
                   Java programming. How would you           accessibility of class members.
                   implement abstraction to solve the        Their primary function is to ensure
                   problem?                                  appropriate encapsulation and data


                                                    361
               Student’s Book  Form Five



     Computer Science Form 5.indd   361                                                     23/07/2024   12:34
   365   366   367   368   369   370   371   372   373   374   375