inheritance

T

Tony Johansson

Hello!

What does this mean.
"The use of inheritance to provide various implementations of a single
abstraction tightly binds these implementations to the abstarction. Therfore
it, is difficult to update the client's code and reuse the abstraction, In
C++, the complete class definition, including the private and protected
sections, is needed to compile the client's code; this leads to a tight
coupling between the abstraction and its implementations, which makes
modifications more difficult."

I thougt inheritance was good it makes code reuse and so on.


//Tony
 
D

Donovan Rebbechi

Hello!

What does this mean.
"The use of inheritance to provide various implementations of a single
abstraction tightly binds these implementations to the abstarction. Therfore
it, is difficult to update the client's code and reuse the abstraction, In
C++, the complete class definition, including the private and protected
sections, is needed to compile the client's code; this leads to a tight
coupling between the abstraction and its implementations, which makes
modifications more difficult."

I thougt inheritance was good it makes code reuse and so on.

I hope the above isn't verbatim -- it's full of typos, punctuation errors and run
on sentences (which makes it confusing).

What it's saying is that inheritance involves strong coupling between base and
derived classes. Why ? Because the class definition of the derived class is
compile-time-dependent on the class definition of the base class.

For example:

class X;
class Y : public X { }; // error: base class `X' has incomplete type

Unfortunately, this is not all that convincing an argument. Even aggregation has
the same requirement:

class X;
class Y { X x; }; // error: field `x' has incomplete type

But aggregation is still a looser coupling than public inheritance, because it's
possible to aggregate a class without being required to provide a sensible
implementation of the base class interface.

The bottom line -- public inheritance is OK, but sometimes it's more appropriate
to use a looser coupling. Public inheritance should primarily be used when you want
to use a class polymorphically, and seldom for anything else, e.g.

void foo (Base& ); // I want to allow foo(x) where x is from my new class
std::list< Base* >; // I want to append Derived* pointers to this list.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top