Re: Private Inheritance and Publice Inheritance
On Sep 4, 12:15 am, SasQ <sa...@go2.pl> wrote:
On Mon, 03 Sep 2007 10:19:45 -0700, karthikbalaguru wrote:
Could someone here tell me some links/pdfs/tutorials
to know about the difference between Private Inheritance
and Public Inheritance?
Inheritance always means "IS A" relationship.
E.g. "Porshe IS A Car".
It means that Porshe could do all the things that
all Cars can do.
C++ inheritance is an implementation technique in C++, to be
used when appropriate. It doesn't "mean" anything, except that
it was a convenient implementation technique. There is not
necessarily a one to one relationship between C++ inheritance
and OO derivation. It is exceedingly rare, of course, for OO
derivation to be implemented with anything other than
inheritance in C++, but I imagine that there are cases. And it
is quite common for C++ inheritance to be used in cases other
than OO derivation.
In practice, if a class B "looks like" a base class (i.e. has a
virtual destructor and at least some virtual functions), and the
class D inherits publicly from B, then unless the documentation
explicitly states otherwise, it's probably safe to assume that
the inheritance is being used for OO derivation, and that the
LSP holds (although of course, the language itself makes no
guarantee). In general, one does not assume OO derivation, the
LSP or "isA" otherwise. (And of course, any explicite
documentation overrides all other considerations.)
Apply public inheritance when you want to describe
"IS A" relationshit to "whole world" ;) By writing
class Porshe : public Car
you say to everyone: "Porshe IS A Car".
Thanks to public inheritance, which defines that
kind of relationship, it's possible to use derived
class [i.e. the Porshe] in every place where the
Car is accepted, because every Porshe is also a Car ;)
Private inherintance could restrict the knowledge
about that family bonds only to the derived class.
That is *not* the conventional use. The "conventional"
statement is that public inheritance is inheritance of
interface, private inheritance inheritance of implementation.
The rest of the world will know nothing about that
relationship. The only thing which will be know that,
will be the class's implementation, and only it
will be able to use from that fact.
Some people also asks what is the difference between
private inheritance and composition. Some of them
sees no difference at all, because they focus only
on the fact that in both cases only the class's
implementation can use the contained/base class.
But there is one, very important difference:
overriding virtual methods. You cannot ovverride
a method of contained class, but you can do it
with methods of the [even privately] interface
derived from a base class.
Quite. Private inheritance implies a somewhat tighter binding
than composition, and the general rule for implementation is to
use composition when you can, and private inheritance when you
have to.
--
James Kanze (GABI Software) email:james.ka...@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34