469,575 Members | 1,658 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Why single inheritance??

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 inheritance?

regards,
Sinex
Nov 16 '05 #1
15 6960
In very simple words-

To make design simpler

For details discussion have a look on http://tinyurl.com/36bzy

--------------------------------------------
Manish Agarwal <ma***********@hotmail.com>
http://personal.vsnl.com/mkag

"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex

Nov 16 '05 #2
Interfaces were introduced as an alternative to multiple inheritance.
While you can only inherit from a single base class, you can implement
as many interfaces as you want.
Nov 16 '05 #3
A lot of people say that if you're using multiple inheritence, then you're
design is probably messed up. While it is debatable, usually if you think
about it a bit you can find a neater (better?) way of achieving what you're
doing without using multiple inheritence.

Interestingly though, it's not like the CLR cannot support multiple
inheritence. Smalltalk achieves this under the CLR, but using a form of
aggregation (delegation) to achieve it. And I'm sure any attempt to use
these objects in other languages will show just how messy the implementation
of multiple inheritence is (I imagine you'll be going through member
variables to get to the parent classes). I believe Eiffel supports multiple
inheritence this way also.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex

Nov 16 '05 #4
John Wood <sp**@isannoying.com> wrote:
Interestingly though, it's not like the CLR cannot support multiple
inheritence. Smalltalk achieves this under the CLR, but using a form of
aggregation (delegation) to achieve it.
That's not the CLR supporting it though - that's Smalltalk effectively
emulating it. It's like claiming that the x86 supports the Z80
instruction set, just because there are Spectrum emulators available :)

The CLR cannot itself support multiple inheritence, IMO.
And I'm sure any attempt to use
these objects in other languages will show just how messy the implementation
of multiple inheritence is (I imagine you'll be going through member
variables to get to the parent classes).
Exactly.
I believe Eiffel supports multiple
inheritence this way also.


Yup, I believe so.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
John Wood wrote:
Interestingly though, it's not like the CLR cannot support multiple
inheritence. Smalltalk achieves this under the CLR, but using a form of
aggregation (delegation) to achieve it.


Can you tell which Smalltalk dialect uses the CLR and offers multiple
inheritance ?
Nov 16 '05 #6
Check out http://www.smallscript.org/ for more info.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:40d84611$1@news...
John Wood wrote:
Interestingly though, it's not like the CLR cannot support multiple
inheritence. Smalltalk achieves this under the CLR, but using a form of
aggregation (delegation) to achieve it.


Can you tell which Smalltalk dialect uses the CLR and offers multiple
inheritance ?

Nov 16 '05 #7
Just my opinion - Multiple inheritance in C++ was nothing but a pain in the
booty. I am glad C# didn't allow it. Although, I believe they were trying to
make a language not as difficult / lower entry level than C++. Another such
language is Java. It has 99.9999% of what you need, and only leaves the
complex parts out. IMHO that's sensible - it leads to better code and
cheaper programmers.

Specifically what problems could Multiple Inheritance cause in .NET. .NET
has somethign called as 'fixed object reference' that is - object references
to the one object in refer to same pointer value regardless of the type that
the reference is being interpreted as. For multiple inheritance, you would
loose that and along with you would loose all base type
interpretations/castings to base types. Secondly, you would loose all highly
efficient pointer comparisons. Yes you have the unsafe block, but who uses
it anyway when it says in red lettering "unsafe" (I had totally forgotten
about "unsafe", until I was reminded of it in these groups). But, shallow
cloning, equality reference etc. none would be as straightforward if you
didn't have fixed object references. Also, for all practical purposes that
MI can be used for, interfaces can do the same, albeit with too many vtable
jumps (3 compared to 1) so it is not as efficient .. but for cleaner code
and better readability, I can live with that.

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/

"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex

Nov 16 '05 #8
>> 'fixed object reference' that is - object references to the one object in
refer to same pointer value regardless of the type that the reference is
being interpreted as <<

wha? :)

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Sahil Malik" <no****@ms.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
Just my opinion - Multiple inheritance in C++ was nothing but a pain in the booty. I am glad C# didn't allow it. Although, I believe they were trying to make a language not as difficult / lower entry level than C++. Another such language is Java. It has 99.9999% of what you need, and only leaves the
complex parts out. IMHO that's sensible - it leads to better code and
cheaper programmers.

Specifically what problems could Multiple Inheritance cause in .NET. .NET
has somethign called as 'fixed object reference' that is - object references to the one object in refer to same pointer value regardless of the type that the reference is being interpreted as. For multiple inheritance, you would
loose that and along with you would loose all base type
interpretations/castings to base types. Secondly, you would loose all highly efficient pointer comparisons. Yes you have the unsafe block, but who uses
it anyway when it says in red lettering "unsafe" (I had totally forgotten
about "unsafe", until I was reminded of it in these groups). But, shallow
cloning, equality reference etc. none would be as straightforward if you
didn't have fixed object references. Also, for all practical purposes that
MI can be used for, interfaces can do the same, albeit with too many vtable jumps (3 compared to 1) so it is not as efficient .. but for cleaner code
and better readability, I can live with that.

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/
"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex


Nov 16 '05 #9
ok everyone seems to have had their word ... Let me approach this
differently.

How is single inheritance with interfaces any different than multiple
inheritance where you only inherit 1 non-pure and many pure abstract bases ?

Many people have suggested that this can be considerred good practice in
multiple inheritance as well since it creates simpler sometimes more
flexible code (per bad design of default behaviors) and good programmers are
expensive. The cost of the simpler code is the ability to provide default
behaviors in your many pure abstracts, if you are designing properly this
will probably not be a major issue.

Cheers,

Greg

"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex

Nov 16 '05 #10
>> How is single inheritance with interfaces any different than multiple
inheritance where you only inherit 1 non-pure and many pure abstract bases ?
<<
Well er, it's basically the same. But that's not what people do when they're
talking about multiple inheritence, they're talking about multiple
implementation inheritence, not interface or abstract class.
Many people have suggested that this can be considerred good practice in multiple inheritance as well since it creates simpler sometimes more
flexible code (per bad design of default behaviors) and good programmers are
expensive. <<

Not sure how the 'and good programmers are expensive' bit links in to the
sentence but...
I agree that those times you want multiple inheritence should be a time to
step back and ask why you want it, and most of the time you end up realizing
that using interfaces and perhaps some aggregation (and encapsulation) is
actually a better solution.

Smalltalk offers multiple inheritence, but then again smalltalk lets you add
methods to existing objects, derive from primitive types and do all kinds of
weird things. It's basically a very liberal OOP language, which is nice n
all, but it comes with a huge performance penalty. And probably a huge
design penalty too.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Greg Young" <gr********@planetbeach.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
ok everyone seems to have had their word ... Let me approach this
differently.

How is single inheritance with interfaces any different than multiple
inheritance where you only inherit 1 non-pure and many pure abstract bases ?
Many people have suggested that this can be considerred good practice in
multiple inheritance as well since it creates simpler sometimes more
flexible code (per bad design of default behaviors) and good programmers are expensive. The cost of the simpler code is the ability to provide default
behaviors in your many pure abstracts, if you are designing properly this
will probably not be a major issue.

Cheers,

Greg

"Sinex" <ma************@honeywell.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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 inheritance?

regards,
Sinex


Nov 16 '05 #11
On 22 Jun 2004 16:20, "Greg Young" wrote:
ok everyone seems to have had their word ... Let me approach this
differently.

How is single inheritance with interfaces any different than multiple
inheritance where you only inherit 1 non-pure and many pure abstract bases ?

Many people have suggested that this can be considerred good practice in
multiple inheritance as well since it creates simpler sometimes more
flexible code (per bad design of default behaviors) and good programmers are
expensive. The cost of the simpler code is the ability to provide default
behaviors in your many pure abstracts, if you are designing properly this
will probably not be a major issue.

Cheers,

Greg


It's probably no different, I would have thought, and is pretty much exactly
what C#/.NET gives you.
But it is different to inheriting multiple non-pure abstract bases which
is the main point, I guess.
--
Simon Smith
simon dot s at ghytred dot com
http://www.ghytred.com/NewsLook - Usenet for Outlook
Nov 16 '05 #12
"John Wood" <sp**@isannoying.com> wrote in message
news:uy**************@TK2MSFTNGP11.phx.gbl...
I believe Eiffel supports multiple
inheritence this way also.


Eiffel implements multiple inheritance by creating shadow interfaces. The
CLR thinks that it's just single inheritance. Inside an Eiffel system, it
looks like MI, so you get the benefits of a language that supports MI
properly.

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
Nov 16 '05 #13
Sinex wrote:
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 inheritance?


Dreaded diamond pattern, for example:

http://www.parashift.com/c++-faq-lit....html#faq-25.8

Although a more comprehensive analysis of the tradeoffs for using multiple
inheritence and other techniques is in the previous FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-25.6

Bottom line: Using MI correctly can be difficult, and the authors of C#
(and Java and more than a few other object languages, for that matter) felt
that it was safer to offer only single inheritance.

Of all the features missing from C#, multiple inheritance hasn't ever seemed
to be that big a deal to me...at least compared to things like
templates/generics (which eliminiates some of the needs for MI anyway.)

--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
Nov 16 '05 #14
John Wood wrote:
Smalltalk offers multiple inheritence,
It doesn't (maybe except S#, which is a Smalltalk derivative that goes
beyond the Smalltalk specification in order to be able to use the CLR)...
but then again smalltalk lets you add
methods to existing objects,
Even at runtime, pretty cool ;-)
derive from primitive types
Smalltalk doesn't have something like a "primitive type", they are
completely normal classes that inherit from "Object", so you can derive from
them as they are nothing special like, say, in Java.
and do all kinds of weird things.
What do you consider "weird things" ?
It's basically a very liberal OOP language, which is nice n
all, but it comes with a huge performance penalty.
It's not that huge anymore. The executing speed with modern VM's comes very
close to Java and C#
And probably a huge design penalty too.


Could you elaborate on this ?

Cheers
Michael
Nov 16 '05 #15
> > And probably a huge design penalty too.

Could you elaborate on this ?
Well 'huge' may be an exaggeration.

From experience I can say that liberal languages are easier to abuse. They
provide so many options, that usually in a moment of weakness the developer
chooses the easiest option, which isn't necessarily the best option.

A classic example is with C (and to a lesser extent C++), which let you do
pretty much anything with a pointer.

The comment stemmed from the thought that both MI, and the concept of adding
methods to existing objects, could easily be abused... and you could easily
end up with a terrible design built from 'easiest route' choices.

Just my opinion and a passing thought.

--
John Wood
EMail: first name, dot, second name at priorganize.com
"Michael Voss" <mi**********@lvrREMOVE.deCAPS> wrote in message
news:40d91efa$1@news... John Wood wrote:
Smalltalk offers multiple inheritence,
It doesn't (maybe except S#, which is a Smalltalk derivative that goes
beyond the Smalltalk specification in order to be able to use the CLR)...
but then again smalltalk lets you add
methods to existing objects,


Even at runtime, pretty cool ;-)
derive from primitive types


Smalltalk doesn't have something like a "primitive type", they are
completely normal classes that inherit from "Object", so you can derive

from them as they are nothing special like, say, in Java.
and do all kinds of weird things.
What do you consider "weird things" ?
It's basically a very liberal OOP language, which is nice n
all, but it comes with a huge performance penalty.


It's not that huge anymore. The executing speed with modern VM's comes

very close to Java and C#
And probably a huge design penalty too.


Could you elaborate on this ?

Cheers
Michael

Nov 16 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Axel Straschil | last post: by
2 posts views Thread by Graham Banks | last post: by
20 posts views Thread by km | last post: by
22 posts views Thread by Matthew Louden | last post: by
5 posts views Thread by AJ | last post: by
47 posts views Thread by Mark | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.