D
Dimitri Maziuk
Mark 'Kamikaze' Hughes sez:
Most OO courses spend a lot of time praising benefits of
inheritance, so people tend to use it where it isn't
entirely appropriate. Or have hard time deciding where it
is appropriate.
E.g. Stack based on Vector -- it "is a" vector, in a sense,
but without random access to the elements. So you either
have to redeclare most Vector's methods as private in
Stack to hide them (you only want isEmpty(), push(),
and pop()) -- that's a lot of typing. If language does
not allow that trick, you'll have to go the other way
and have BaseVector with everything protected, from
which you derive both Stack and Vector. That's just as
much typing, but this time redeclaring protected BaseVector
methods public in Vector, plus an extra class.
Google for oop composition vs inheritance, it should turn
up a better example. And a few other reasons for using
composition instead of inheritance.
....Java does, of
Well, I'd say interface is a behaviour specification that
has nothing to do with inheritance. As an added bonus it
allows you to fake multiple inheritance without having to
deal with its problems. That definitely has its pluses, yes.
Dima
Dimitri Maziuk <[email protected]>
Hmn. My other main OOP experience was with Objective C, which has a
somewhat Java-like inheritance system, but I just can't see the value of
creating things by composition that aren't composite.
Most OO courses spend a lot of time praising benefits of
inheritance, so people tend to use it where it isn't
entirely appropriate. Or have hard time deciding where it
is appropriate.
E.g. Stack based on Vector -- it "is a" vector, in a sense,
but without random access to the elements. So you either
have to redeclare most Vector's methods as private in
Stack to hide them (you only want isEmpty(), push(),
and pop()) -- that's a lot of typing. If language does
not allow that trick, you'll have to go the other way
and have BaseVector with everything protected, from
which you derive both Stack and Vector. That's just as
much typing, but this time redeclaring protected BaseVector
methods public in Vector, plus an extra class.
Google for oop composition vs inheritance, it should turn
up a better example. And a few other reasons for using
composition instead of inheritance.
....Java does, of
course, allow multiple inheritance in the only way that's safe: through
interfaces.
Well, I'd say interface is a behaviour specification that
has nothing to do with inheritance. As an added bonus it
allows you to fake multiple inheritance without having to
deal with its problems. That definitely has its pluses, yes.
Dima