0% found this document useful (0 votes)
3 views1 page

Code Reuse: Composition vs. Inheritance

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 views1 page

Code Reuse: Composition vs. Inheritance

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

Abstract Inheritance

12 December 2024 00:12

Why do we prefer code reuse via composition over inheritance?

Advantage using inheritance:


that it is done statically at compile time and easy to use

Disadvantage using inheritance:


it is a static implementation inherited from a parent class and cannot be changed at runtime
Subclass become dependent on parent class this makes harder to reuse the subclass

Code reuse via composition(Object Composition)


Functionality is acquired dynamically at runtime by object collecting reference to other object

Advantage:
is that implementation can be replaced during runtime
Therefore whenever possible use object composition over inheritance

New Section 1 Page 1

You might also like