0% found this document useful (0 votes)
3 views13 pages

Advanced OOP1

for advance oop

Uploaded by

Rhuda Santillan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Advanced OOP1

for advance oop

Uploaded by

Rhuda Santillan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Inheritance vs

Composition
By: Francisco B. Bacamante Jr., DIT
• Objective: Understand the differences, advantages, and use
cases of composition and inheritance in software design.

• Context: Both composition and inheritance are fundamental


concepts in OOP that help manage code reuse and modularity.
Recap of Basic OOP Principles

• Classes and Objects: The building blocks of OOP.


• Core Concepts: Encapsulation, Abstraction,
Inheritance, and Polymorphism.
• Why This Matters: Helps in structuring code
efficiently and managing complexity.
Understanding Inheritance
Definition: Inheritance allows one class (subclass/child) to inherit
properties and behaviors from another class (superclass/parent).
Understanding Composition
Definition: Composition involves constructing classes using other classes as
components, creating a "has-a" relationship.
Comparison of Composition and Inheritance
Best Practices and When to Use Each
Real-World Examples and Design Patterns

Inheritance:
• Used in the Template Method Pattern where a base class
defines the structure and subclasses implement specific
behaviors.

Composition:
• Used in the Strategy Pattern, where different behaviors are
encapsulated in separate classes and can be dynamically
assigned.
Case Study

Scenario: Designing a software system for a vehicle.


Inheritance Approach: Using a Vehicle superclass with Car and Bike subclasses.
Composition Approach: Creating a Vehicle class composed of components like
Engine, Wheels, and Transmission that can be customized for different vehicles.
Discussion: Which approach provides more flexibility and maintainability?
PROS and CONS

Inheritance:
• Pros: Simple, intuitive, reduces code duplication.Cons: Can lead to
tight coupling, less adaptable to change.

Composition:
• Pros: Flexible, modular, encourages code reuse.Cons: More initial
setup, can add complexity.
Answer the following questions:
1) Explain the main differences between inheritance and composition in OOP. What are the
implications of using one over the other in terms of code flexibility and maintainability?
2) Discuss a scenario where using inheritance can lead to code rigidity and potential issues in
future modifications. How could composition be used as an alternative solution
3) Analyze the impact of inheritance and composition on code reusability and scalability. Which
approach is more advantageous in developing large-scale software systems and why

You might also like