"DaKoadMunky" <da*********@aol.com> wrote in message
news:20***************************@mb-m12.aol.com...
The benefit of doing this is to keep the inheritance hierarchy at a low depths!
Why is this a benefit?
When is such a rule best applied?
Citing your example, what if there is a legitimate "isa" relationship
between class C and class B? Do you still support class C containing a B and
delegating to it and providing appropriate conversion operators so that
the substitutiability of a C for a B is maintained?
Hi,
this is a contentious issue in the world of OO and is called inheritance vs
delegation (composition).
In fact if there is a genuine isa relation between c and b classes, thats
when delegation comes to the
rescue. The advantage of using delegation is as follows:
Ask your self the question why would C be isa B? Well maybe because B has
say 3 virtual (or may not be virtual)
that it uses as is (does not override) and in addition has 2 other methods
of its own. Rite? Now in this case since
C uses the same interface as B(refer to the initial eg i d given), therefore
it still has the 3 methods of B only that
inside each of these methods , it invokes a contained B objects
corresponding methods. with me so far?
Question is what is the advantage of doing this. wait, lets see the
disadvantages of this first!! You are increasing the
size of the object. Since you are delegating, therefore there is an extra
overhead involved, so purists could even
argue that it is making things slower . Correct!!
Advantage? Consider that instead of class B, there is a class D which has
hte same methods of B but implements
them differently. At runtime you want your class C to choose either the
implementation of B or D . Thats where
delegation comes to the rescue. Delegation gives you runtime flexibility
whereas inheritance gives you compile
time flexibility.
Last question? In the original example i have talked about inheriting from
interface (aka abstract class with only
pure virtual methods). So each class inherits from the interface. Isnt this
inheritance too? shouldnt we get rid of this
too? the answer is, try and differentiate between class inheritance and
interface inheritance. Interfae inheritance is
what i just mentioned. Class inheritance is what we got rid of .