From: Intransition Date: 2010-11-03T01:37:27+09:00 Subject: Re: the dark side of inherited methods On Nov 2, 5:31 am, Robert Klemme wrote: > On Tue, Nov 2, 2010 at 4:29 AM, Tony Arcieri wrote: > > On Sun, Oct 31, 2010 at 5:49 PM, James Edward Gray II >> wrote: > > >> It is a problem with inheritance and another reason why inheritance is > >> almost never what we want. > > > That's a pretty extreme position to take. Inheritance is an indispensable > > tool. That said, I will agree that "is a" relationships come up far less > > frequently than 'has a" relationships. > > I believe this is what James wanted to stress: inheritance is used far > too often for purposes where it's not appropriate.  A real is_a > relationship is pretty rare yet many people use inheritance, > especially with core / std lib classes as parents which almost always > is a code smell. > > Side note: there are languages such as Eiffel and C++ which have > support for implementation inheritance (i.e. the interface changes > incompatibly from super class to sub class) and in those languages > inheritance can be used more often than in language which lack these > features (e.g. Java). Indeed, I think inheritance gets a really bad rap in Ruby b/c Ruby's base classes and inheritance system are so poorly designed to handle it. The other day I was talking to my Father, a Cobol programmer from back in the day, and he was telling me that when he retired, OOP was just starting to get hyped. He was quite interested in it at the time and then rattled off some of the advantages he remembered it was to bring to the field. Inheritance for code reuse was high on the list and related to that, one point eally struck me -- instead of versioning code as we have become accustomed, the idea was to subclass the original class and implement changes in the subclass. Using some sort of class name referencing scheme you could use any version. It sort of reminded me of database migrations. I imagined is something like: class Foo1 .. end class Foo2 < Foo1 ... end #pick a version Foo = Foo2 Sadly, I had to inform him that OOP did not quite prove to be the godsend everyone originally thought it might be.