PG 8
PG 8
Suggested Solution: The key principles of OOP are encapsulation, inheritance, polymorphism,
and abstraction. Encapsulation involves bundling data and methods that operate on that data
within one unit (class). Inheritance allows a new class to inherit properties and methods from an
existing class. Polymorphism enables objects to be treated as instances of their parent class,
while abstraction focuses on hiding complex implementation details and exposing only the
necessary features of an object.
2. Understanding
3. Applying
Question: Provide an example of how you would use inheritance in a PHP project to model
different types of vehicles.
Suggested Solution: In a PHP project, we could create a base class Vehicle with properties like
make, model, and year. Then, we could create subclasses such as Car, Truck, and Motorcycle
that inherit from the Vehicle class. Each subclass could have specific properties (e.g., Car could
have a numberOfDoors property) and methods (e.g., Truck could have a loadCargo() method)
that are unique to that type of vehicle while still sharing the common functionality of the
Vehicle class.
4. Analyzing
Question: Compare and contrast encapsulation and inheritance. In what scenarios would you
choose one over the other?
Suggested Solution: Encapsulation focuses on restricting access to an object's internal data and
exposing only what is necessary, promoting data integrity. In contrast, inheritance allows for
code reuse and the establishment of a hierarchy between classes. One might choose
encapsulation when data security and integrity are a priority, such as when dealing with sensitive
user information. Inheritance would be favored in scenarios requiring code reuse and hierarchical
relationships, such as creating a set of related classes with shared functionality.
5. Evaluating
Question: Evaluate the impact of using inheritance versus composition in software design. What
are the pros and cons of each?
Suggested Solution: Inheritance allows for clear hierarchical relationships and code reuse,
making it easier to implement shared behaviors. However, it can lead to tight coupling and
fragility, where changes in the parent class can affect all child classes. Composition, on the other
hand, promotes loose coupling and flexibility, allowing for the dynamic combination of
behaviors at runtime. However, it can lead to more complex code structures and may require
more boilerplate code to manage relationships. The choice between them often depends on the
specific needs of the application and the desired maintainability.
6. Creating
Question: Design a simple class hierarchy for a library management system. Include at least
three classes and describe their relationships.
Suggested Solution: We could create a base class LibraryItem with properties like title,
author, and isbn. Subclasses could include Book and Magazine, which inherit from
LibraryItem. The Book class could have additional properties like numberOfPages, while the
Magazine class could have a publicationDate property. Additionally, we could create a
Library class that holds an array of LibraryItem objects, allowing users to add, remove, or
search for items within the library system. This design promotes code reuse and a clear structure
for managing different types of library items.
These questions encourage deeper engagement with the subject matter and promote higher-order
thinking, aligning with Bloom's Taxonomy.
4o mini