[Tutor] Follow up to 'class data'

dman dsh8290@rit.edu
Tue, 4 Dec 2001 15:49:00 -0500


On Tue, Dec 04, 2001 at 01:02:45PM -0500, Andrei Kulakov wrote:
| On Tue, Dec 04, 2001 at 05:33:28PM -0000, alan.gauld@bt.com wrote:
| 
| [snip]
| 
| > Interestingly there are some objective data to suggest that 
| > inheritance can actually be detrimental to maintenance since
| > it is much harder to ensure that a code fix in one place won't 
| > cause damage some place else that inherits the changed code.
| 
| Let's say you inherit B from class A. If you change A and it
| messes up B, the way I see it, could mean 2 things: either
| B isn't really a subset of A and shouldn't have been inherited
| from it, or the change was too broad, and you should've instead
| derived another child class C from A and changed C.. That's
| theory, I don't know how it pans out in practice.. and you know
| what they say about the difference between theory and practice:
| in theory, there is no difference, but in practice, there is :P

My profs prefer "composition" over "inheritance".  The truth of the
matter is that inheritance can be a convenient way to automatically
get a bunch of implementation or to specialize something (like, maybe
subclass 'int' so that you can add some special operation).  However
inheritance also creates tight coupling and can make maintenance
difficult if the classes and inheritance tree were not ideal from the
start.

Object composition is a technique where instead of inheriting from a
class, instead an instance of that class is stored in the object and
operations are delegated to it.

If you do enough Java programming you will see how inheritance is not
flexible enough.  Composition can be a pain to code (especially for
large sets of operations) in static languages like C++ and Java, but
with Python you can use __getattr__ to automatically delegate all
operations that aren't implemented in the current class.

-D

-- 

But As for me and my household, we will serve the Lord.
        Joshua 24:15