-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?

0

2 Answers 2

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.

0
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.

2
  • sorry but we studied two forms inheritance and inclusion/composition they can't be the same, and that's what i'm asking about
    – younes
    Commented Nov 30, 2014 at 2:29
  • 3
    There is no such thing as composition inheritance. Composition is when an object is composed of other objects which it "owns" or directly controls. I really think you are thinking of aggregation though, which is a form of composition but the object does not own the objects it uses. However, is it also possible you are thinking of constraint-based inheritance? That's a form of inclusion inheritance.
    – tbsdy
    Commented Nov 30, 2014 at 2:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.