Page 224 - Computer_Science_F5
P. 224
2. Explain how encapsulation is these inherited traits and potentially
achieved in the Book class of add its own unique characteristics
the library management system (wearing rubber, jogging) to become
scenario. something more specific. Chapter Four: Object oriented programming with C++
3. How do getters and setters
FOR ONLINE READING ONLY
contribute to encapsulation in OOP, The following is an example of inheritance
and what is their role in accessing in real world:
and modifying object state? Imagine a class called “Animal.” This
4. In the Car class example, how are class would involve shared attributes
encapsulation principles applied to of animals such as name, type (such
control access to the car’s attributes as, mammal, bats), and a makeSound()
and methods? method.
5. Can you describe a real-world Now, a class called “Cat” can inherits from
scenario where encapsulation “Animal.” The “Cat” class automatically
would be beneficial, based on the acquires all the properties (attributes) and
examples provided in the code?
methods from the “Animal” class. This
eliminates the need to rewrite code for
Inheritance common functionalities.
Inheritance, is a core principle in OOP
that enables a new class to acquire Additionally, the “Cat” class can introduce
attributes and methods from an existing its own distinct attributes and methods.
class, referred to as the superclass or For instance, it might include a breed
parent class. This facilitates the reuse attribute and a vocalise () method
of code and facilitates the establishment specifically tailored for cats.
of hierarchical connections between
classes. (c) Categories of Inheritance: There
(a) Parent (Superclass): Think of are four categories of inheritance.
an existing class as a parent class Figure 4.3 shows these categories of
(superclass). It has its own properties inheritance.
and behaviors (such as nose, color Inheritance
or eating ability). The superclass
generously allows other classes to
inherit these traits. Single Multilevel Hierarchical Hybrid
(b) The inheriting child (Subclass): A Figure 4.3: Categories of inheritance in OOP
new class (subclass) can act like a
child, inheriting the traits (such as (i) Single inheritance: This type of
nose, color, eating ability) from the inheritance enables a subclass to inherit
superclass. This subclass can then use the properties and behaviors of a single
215
Student’s Book Form Five
Computer Science Form 5.indd 215 23/07/2024 12:33

