473,324 Members | 2,313 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,324 software developers and data experts.

Unintended inheritance

The problem of unintended inheritance is typical for OO frameworks and
can be explained as follows. Given a class Foo implemented by Alice and
a derived class Bar of Foo implemented by Bob. Bar implements a method
f. In a later version of Foo Alice also implements a method f. The f of
Bar overrides the f of Foo unintentionally. If an instance bar of Bar
will be created and some method g of Foo calls f it is the wrong f,
namely the one of bar will be called.

To prevent unintended inheritance C# introduced a relatively complex
contract semantics using three modifiers virtual, override and new. Is
this issue serious for Python programmers, in particular for those
working in larger projects ( Twisted, Zope ... ) or is it not? If not,
why not?

Kay

Feb 20 '06 #1
2 1319
Kay Schluehr <ka**********@gmx.net> wrote:
...
To prevent unintended inheritance C# introduced a relatively complex
contract semantics using three modifiers virtual, override and new. Is
this issue serious for Python programmers, in particular for those
working in larger projects ( Twisted, Zope ... ) or is it not? If not,
why not?


I have not found it to be a particular problem (in C++ as well as in
Python or Java), because subclasses' coupling to superclasses is too
strong anyway to expect everything to just work smoothly when the
superclass's interface is changed, and even the most semidecent of
unittests easily catch this specific problem -- obviously all subclasses
must be recompiled and tested when a superclass's interface is changed
in any way. Other kinds of coupling are subtler and more problematic
(not so much in Python as in C++, I'd say, but it's arguable).

Lakos' book on large-scale design for C++ has good advice on dependency
control, which boils down to (WAY oversimplifying): each component must
expose to all others only unchanging abstract interfaces; anything
depending on anything beyond an abstract interface (as a subclass must)
must be in the same component as what it depends on. Inheritance, while
way handy, introduces too-strong coupling (==dependencies) to be freely
used across independently developed and maintained components; and any
change in interface (I would argue, in fact, most any change in
PROTOCOL, which is a stronger constraint) must occur on new, separate
interfaces (which may well extend the existing ones, of course).

Yes, frameworks (separately developed, large components which are
*supposed* to be used via inheritance by separately developed and
maintained components) are quite problematic in terms of Lakos' rules
and guidelines (if you're at all interested in these subjects you should
surely read Lakos' book, anyway: no summary can do it justice). But I
believe (with no real-world experience of C#, admittedly) that they will
remain essentially just as problematic with C#'s complex rules -- a new
release of a framework, which changes protocols, will still be able to
break uncountable independently developed and maintained smaller
components, and the fact that the compiler catches the breakage (if, of
course, the other components DO get recompiled) rather than relying on
semidecent unittests is scant consolation -- there's still no bound to
the amount of work that may be needed to repair the wreckage.
Alex
Feb 20 '06 #2

Alex Martelli wrote:
Kay Schluehr <ka**********@gmx.net> wrote:
...
To prevent unintended inheritance C# introduced a relatively complex
contract semantics using three modifiers virtual, override and new. Is
this issue serious for Python programmers, in particular for those
working in larger projects ( Twisted, Zope ... ) or is it not? If not,
why not?


I have not found it to be a particular problem (in C++ as well as in
Python or Java), because subclasses' coupling to superclasses is too
strong anyway to expect everything to just work smoothly when the
superclass's interface is changed, and even the most semidecent of
unittests easily catch this specific problem -- obviously all subclasses
must be recompiled and tested when a superclass's interface is changed
in any way. Other kinds of coupling are subtler and more problematic
(not so much in Python as in C++, I'd say, but it's arguable).

Lakos' book on large-scale design for C++ has good advice on dependency
control, which boils down to (WAY oversimplifying): each component must
expose to all others only unchanging abstract interfaces; anything
depending on anything beyond an abstract interface (as a subclass must)
must be in the same component as what it depends on. Inheritance, while
way handy, introduces too-strong coupling (==dependencies) to be freely
used across independently developed and maintained components; and any
change in interface (I would argue, in fact, most any change in
PROTOCOL, which is a stronger constraint) must occur on new, separate
interfaces (which may well extend the existing ones, of course).

Yes, frameworks (separately developed, large components which are
*supposed* to be used via inheritance by separately developed and
maintained components) are quite problematic in terms of Lakos' rules
and guidelines (if you're at all interested in these subjects you should
surely read Lakos' book, anyway: no summary can do it justice). But I
believe (with no real-world experience of C#, admittedly) that they will
remain essentially just as problematic with C#'s complex rules -- a new
release of a framework, which changes protocols, will still be able to
break uncountable independently developed and maintained smaller
components, and the fact that the compiler catches the breakage (if, of
course, the other components DO get recompiled) rather than relying on
semidecent unittests is scant consolation -- there's still no bound to
the amount of work that may be needed to repair the wreckage.
Alex


Interesting statement, Alex, as always. I've found an interview with
Anders Hejlsberg from 2003 on Artima where he defended his C# design
decisions with regard to the problem of "unintended inheritance":

http://www.artima.com/intv/nonvirtual.html

A strange twist is Anders exemplification of "pragmatism" with which
Bruce Eckel seems to agree emphatically. I would indeed assume the
exact opposite: laisser faire by pragmatists and expressive control by
academics. I'm also not sure what I should think about the confusing
use of the word "versioning" that is placed concretely by Bruce Eckel (
"DLL hell" ) but more metaphorically by Anders. His remarks about the
difference of ingoing and outgoing interfaces are interesting but
remain a little underdeveloped.

Kay

Feb 24 '06 #3

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
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...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
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...
1
by: sotec | last post by:
erroe message and code portion is like this.... how can i change this code to not occur to error if (page.Session == null || page.Session != userId ) { UserStatusSetting(page, userId); } ...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.