473,399 Members | 4,177 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,399 software developers and data experts.

inheritence vs composition

Hi,

This question is more towards design than towards c++ details.

By looking at books on design patterns and various google threads on
the same topic, I see that composition is favoured to inheritence. One
more article I read was Allen holub's "Why extends is evil".
http://www.javaworld.com/javaworld/j...1-toolbox.html
One general advice I found was that - extending behaviour is better
done with interfaces and composition.

My question is - can we totally get rid of inheritence from concrete
classes ?

Because in all cases we can put common code in a class and use it by
containing an instance to it rather than subclassing it. Also I think
we do subclassing to get a modified behaviour and nothing else. The
only problem I see is few more lines of code. But the advantage is that
even the common code can be replaced at runtime by changing the
instance at runtime.

Is it a good idea if I almost never use subclassing for specializing
the base class, but just to get a interface.
Regards
Preetham

Jul 23 '05 #1
7 1944
On 25 Jan 2005 00:45:28 -0800, preetam <pr******@yahoo.co.in> wrote:
Hi,

This question is more towards design than towards c++ details.

By looking at books on design patterns and various google threads on
the same topic, I see that composition is favoured to inheritence. One
more article I read was Allen holub's "Why extends is evil".
http://www.javaworld.com/javaworld/j...1-toolbox.html
One general advice I found was that - extending behaviour is better
done with interfaces and composition.

My question is - can we totally get rid of inheritence from concrete
classes ?
"Why extends is evil": such a general statement is certainly wrong. one
always has to differentiate.

a very nice example of (even multiple) concrete subclasses is
building/extending/customising a class by deriving it from policy classes,
as suggested by Alexandrescu in "Modern C++ Design".

Because in all cases we can put common code in a class and use it by
containing an instance to it rather than subclassing it. Also I think
we do subclassing to get a modified behaviour and nothing else. The
only problem I see is few more lines of code. But the advantage is that
even the common code can be replaced at runtime by changing the
instance at runtime.


imho, this last suggestion comes close to Sutter's Pimpl-idiom (see "More
(?) Exceptional C++").

--
have a nice day
ulrich
Jul 23 '05 #2
You will end up doing a lot more work. Consider the 'std::streambuf'
"protocol". It contains several protected virtual methods, which come
with default implementations. If you were just using interfaces, you
would have to implement every virtual method, even if just to delegate
to a default implementation.

I think, "Yes," you can replace inheritence form concrete classes with
inheritence from interfaces, but you lose a lot of convenience. (OTOH,
mayber there is a counterexample...) /david

Jul 23 '05 #3
preetam wrote:
Hi,

This question is more towards design than towards c++ details.

By looking at books on design patterns and various google threads on
the same topic, I see that composition is favoured to inheritence. One
more article I read was Allen holub's "Why extends is evil".
http://www.javaworld.com/javaworld/j...1-toolbox.html
One general advice I found was that - extending behaviour is better
done with interfaces and composition.

My question is - can we totally get rid of inheritence from concrete
classes ?

Because in all cases we can put common code in a class and use it by
containing an instance to it rather than subclassing it. Also I think
we do subclassing to get a modified behaviour and nothing else. The
only problem I see is few more lines of code. But the advantage is that
even the common code can be replaced at runtime by changing the
instance at runtime.

Is it a good idea if I almost never use subclassing for specializing
the base class, but just to get a interface.

Let me give you a practical example on how I face inheritance in
application programming and perhaps you could provide me your alternative.
When I write .NET GUI applications I create a class MyForm: public Form,
which form contains:

http://msdn.microsoft.com/library/de...mberstopic.asp
All its members are already defined in Form making it convenient to use
them directly, and we only add other components we need in our derived
class, like two Button *, one Label * and so on, which also have their
members defined.

And this is the case for all such component classes.

How could we substitute this approach with the one you are mentioning,
without a major impact in productivity and convenience?


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #4
Yes, convenience seems to be a driving factor. /david

Jul 23 '05 #5
"preetam" <pr******@yahoo.co.in> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,

This question is more towards design than towards c++ details.

By looking at books on design patterns and various google threads on
the same topic, I see that composition is favoured to inheritence. One
more article I read was Allen holub's "Why extends is evil".
http://www.javaworld.com/javaworld/j...1-toolbox.html
One general advice I found was that - extending behaviour is better
done with interfaces and composition.

My question is - can we totally get rid of inheritence from concrete
classes ?


Without inheritance you have no polymorphism, unless you want to implement
it in a much less convenient way, but why would you want to do that? The
more objects you have (i.e., by using composition), the more potential
polymorphic behaviour you have if each object's type can be any one of a
number of different classes in a hierarchy, chosen at run-time. So, a
combination of both composition and inheritance can give you far more
flexible run-time behaviour than either one on its own.

DW

Jul 23 '05 #6
Ulrich Achleitner wrote:
imho, this last suggestion comes close to Sutter's Pimpl-idiom (see
"More (?) Exceptional C++").


Also known as the "Bridge" pattern (see GoF).
Jul 23 '05 #7
Thanks to all who responded.
Now I see why we can't totally remove inheritence.

In case of forms example above, I might endup delegating every single
method on Forms class. Aso the number of classes would be more. One
morre thing is the user has to call few set methods to change the
instance if needed. I might have a performence issue too.

But at the same time it gives more floxibility.

So there must be a nice balance between - performence, simplicity of
use/understanding, flexibility. It must be flexible but not at the cost
of making it too complex to understand and rewriting code. right..?
--
Preetam

Jul 23 '05 #8

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

Similar topics

4
by: fog | last post by:
Given: class A; and B "has-a" A. The composition relationship between A and B can be implemented in three ways: =================== # 1. class B {
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
4
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition...
5
by: Neelesh Bodas | last post by:
This might be slightly off-topic. Many books on C++ consider multiple inheritence as an "advanced" concept. Bruce Eckel says in TICPP, volume 2 that "there was (and still is) a lot of...
6
by: toton | last post by:
Hi, I am inheriting a vector<Pointclass as a PointVector non-polymorphically, to add several additional functionalities to it. I know the dangers of inheriting a container class, as it doesn't...
2
by: Gary Wessle | last post by:
Hi what is the difference "in code writing" between aggregation and composition? is the following correct? aggregation (life time of both objects are independent). class A{ /* ... */ };...
3
by: jyck91 | last post by:
I want to get some ideas to organise a composition analyzer?? eg. frequencies of letters Thx for your suggestion
5
by: jyck91 | last post by:
Can anyone give me some ideas to organise a composition analyzer in C? It analyses an english composition. What functions should it include? eg. frequency of letter/ words thanks for help
7
by: ManicQin | last post by:
Hello All I hope you could help me. Let's examine the next inheritance tree: class A { public: void init() { // Do things initialiseClass(); }
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.