-1

the question is self explanatory, I just want to know when to use the inclusion and when to use inheritance, and which one serves for re-usability.

in other words, which one meets the Object Oriented programming principles the most?

Dylan Meeus
  • 675
  • 1
  • 6
  • 18
younes
  • 123

2 Answers2

2

In general I would agree to favor composition over inheritance. You can think of composition as a "has-a" relationship and inheritance as an "is-a" relationship. Composition helps manage complexity by keeping each class separate and only aggregating what you need.

One drawback to composition is that you may find yourself having to write a number of proxy "forwarding" methods for your composed classes.

I find a good rule of thumb is that if a class really is a type of the base class and needs most or all of the base class functionality exposed, these are good reasons to consider using inheritance.

gnat
  • 20,543
  • 29
  • 115
  • 306
scotru
  • 238
  • 3
  • 8
1

I think you might be confusing the terms. Inclusion is actually a form of inheritance, it basically says that if you a type T, then subtype T1 and subtype T2 can be sent the same messages.

Perhaps you wanted to know the difference between object aggregation/composition and inheritance? If so, then in general favor aggregation and composition over inheritance asit often prevents restructuring base classes if requirements change.

If you want to know more, then I recommend reading the GoF (Gang of Four)'s book Design Patterns: Elements of Reusable Design, or try Head First Design Patterns which is a bit easier to read and understand.

tbsdy
  • 168