473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

inheritance is not for code-reuse (??)

I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lit....html#faq-30.4)

This confused me somewhat as I have always thought you get code reuse
"for free" with inheritance. Am I missing something?. Will someone care
to explain ??
May 3 '07 #1
6 3776
On May 3, 10:52 pm, Bart Simpson <123evergr...@terrace.comwrote:
I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lit....html#faq-30.4)

This confused me somewhat as I have always thought you get code reuse
"for free" with inheritance. Am I missing something?. Will someone care
to explain ??
The object models of Smalltalk and C++ have one big difference.
Smalltalk uses something called operational polymorphism, often these
days called duck typing. C++ on the other hand uses inclusional
polymorphism which is a form of type constraint.

This basic difference in the type models/message dispatchers in the
two languages makes a huge difference in how classes are used in
practice, even though they can both be shown to be equivalent (like a
Turing machine being provably equivalent to Church's lambda calculus -
they're the same in one sense, but the way a problem is approached in
each is still completely different).

I think the FAQ is only half right. Most people most of the time use
classes in the way that the FAQ describes in the two languages, but
this is really habitual rather than imposed by the object models of
the languages.

You do get code re-use for free with inheritance, but to get the sort
of re-use you get with Smalltalk in C++ you have to use templates. The
STL makes use of re-use in (pretty much) the same way that the
Smalltalk containers do.

In C++ though a class hierarchy is most often used to constrain the
types that a function will use (and Java uses hierarchy in this way
too). This constraint cannot be used in Smalltalk whose message
dispatcher ignores the actual type of the receiver (well, of course it
doesn't exactly, but compared to C++ it does).

I've not really read through the C++ FAQ's comparison with Smalltalk
before and I'm not entirely sure that it is explained as well as it
could be. Something I'll have to study and think about.

I'm not sure if this really answers your question or not either. The
two languages are quite different in the ways that they are normally
used, but each language can "meet the other" if the context is right.
K

May 3 '07 #2
On May 3, 5:52 pm, Bart Simpson <123evergr...@terrace.comwrote:
I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lit....html#faq-30.4)
This confused me somewhat as I have always thought you get code reuse
"for free" with inheritance. Am I missing something?. Will someone care
to explain ??
It depends on what you use inheritance for. In most modern OO
langauges (C++, but also Java, C#, Eiffel, Ada 95...), most
inheritance is inheritance of interface ; a class implements a
specific interface.

In older OO languages (e.g. Smalltalk), there was no static type
checking, and so no need for inheritance of interface. (Put in
other words: you find out about your errors at run-time, not at
compile time.) Historically, too, the coupling inherent in
inheritance wasn't recognized when OO was young, so inheritance
was used a lot when today we would prefer composition.

Basically, code reuse occurs when class A contains an instance
of class B, whether it be by inheritance or composition, and
class B contains implementation code used in the implementation
of class A. Because inheritance implies stronger coupling than
composition, composition is preferred IF all other things are
equal. If they aren't inheritance can be, and still is, used
for this; in C++, such inheritance is generally private.

In most cases of public inheritance (but there are exceptions,
e.g. the templat pattern), the base class contains no
implementation code; it only defines an interface. (It may
contain code defining this interface, for example verifying pre-
or post-conditions.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 4 '07 #3
"Bart Simpson" <12**********@terrace.comwrote in message
news:1O******************************@bt.com...
>I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lit....html#faq-30.4)

This confused me somewhat as I have always thought you get code reuse "for
free" with inheritance. Am I missing something?. Will someone care to
explain ??
I don't know for sure, can only say my own thoughts.

When a class is designed with inheritance, that's what it's designed for.
The bases and derives usually get coupled fairly tightly unless you
purposely try not to (and I guess are using Composition, whatever that is
:D ).

A base class is usually specifically designed for the types of derived
classes that will use it. Additionally, derived classes are usually
specifically designed for the base class they derive from.

The problem comes in when in another program you decide to try to reuse a
polymorphic class for another purpose. You'll usually wind up redesigning a
good portion of the base class and have to create a new derived class.
Unless you specifically design the base class to be more generic.
May 5 '07 #4
On May 6, 5:47 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>... unless you
purposely try not to (and I guess are using Composition, whatever that is
:D ).
Inheritance:

MyList : private std::list< int {
public:
using begin();
};
Composition:

MyList {
public:
iterator begin() { return m_list.begin(); }
private:
std::list< int m_list;
};
Composition is more work (or at least more typing), but the designs
are generally better. To derive from list non-privately (protected
might just barely be OK) in this sort of situation should be
considered an error (even if it doesn't cause bugs).

Smalltalk doesn't have private inheritance and it doesn't have
inclusional polymorphism so the issues are somewhat different in that
language.
K

May 6 '07 #5
On May 3, 8:52 am, Bart Simpson <123evergr...@terrace.comwrote:
I remember reading on parashift recently, that "Composition is for code
reuse, inheritance is for flexibility" see
(http://www.parashift.com/c++-faq-lit....html#faq-30.4)

This confused me somewhat as I have always thought you get code reuse
"for free" with inheritance. Am I missing something?. Will someone care
to explain ??
"C++ Coding Standards" (Sutter and Alexandrescu) explains the
reasoning behind this advice, and cites as its basis, the "Liskov
Substitution Principle". Essentially, the LSP states that inheritiance
implies substitutability. So if A is a B, then A may inherit from B,
but if A just wants to re-use B's code, then A should make B a class
member.

I can also endorse this advice from my own experience. Having once
worked on a large software project that made heavy use of inheritance,
I can attest to the problems that can arise if perfect
substitutability is not required of a derived type. Without
substitutability, the small differences between base and derived
objects lead either to subtle bugs or otherwise require special
handling that violates encapsulation and that often requires a type-
specific implementation.

In fact, I would describe the problem like this: when inheriting from
a base class, the derived class effectively re-uses all of the base
class's implementation. But it is often the case that the programmer
really wants to re-use only a portion of the base's code - and not its
entire implementation. So what often happens in this situation is that
the programmer has to write code to prevent the derived class from re-
using code in the base class in ways that were neither anticipated nor
intended - so the programmer winds up writing more code - not less.

Greg
May 6 '07 #6
On 6 May 2007 00:02:29 -0700, Greg Herlihy <gr****@pacbell.netwrote:
>In fact, I would describe the problem like this: when inheriting from
a base class, the derived class effectively re-uses all of the base
class's implementation. But it is often the case that the programmer
really wants to re-use only a portion of the base's code - and not its
entire implementation. So what often happens in this situation is that
the programmer has to write code to prevent the derived class from re-
using code in the base class in ways that were neither anticipated nor
intended - so the programmer winds up writing more code - not less.
I too have encountered projects where public inheritance is used simply to
reuse code, without regard to the LSP. The problem is that such use leads very
quickly to "meaningless" (semantically nonsensical) relationships, which are
impossible to extend any further, and are likely to break algorithms that use
the base class as an abstraction. Whatever reuse you get out of the
inheritance relationship is usually small compared to the fact that you can no
longer use the base class as an abstraction in the rest of your program.

The LSP guarantees that inherited classes will not break algorithms that refer
to base classes polymorphically. This is the basis of a large portion of
successful real-world algorithm reuse that I have encountered.

Composition allows you to use existing code without subverting their
inheritance relationship. The new class you create can be part of a completely
separate inheritance tree, yet use all the facilities of the class you reuse.

-dr
May 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Hunter | last post by:
I am using pycxx 5.2.2 to implement some extension code and have a problem relating to inheritance. I have a pure virtual base class and two concrete derived classes. In the code below, everthing...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
15
by: Sinex | last post by:
Hi, Why does C# disallow multiple inheritance? Whats the reason behind this? Is there any advantage or is it just a method to avoid some problems (if so, what problems?) that come with multiple...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.