in reply to Re: Understanding 'Multiple Inheritance'
in thread Understanding 'Multiple Inheritance'
The heart of the big benefits from OO is encapsulation, not inheritance.
The heart of the big benefits from OO is polymorphism. Polymorphism encourages encapsulation, but absolutely requires inheritance.
Polymorphism allows you to derive (inherit) from a class and change its behaviour for certain methods. And that is the basic advantage of OO.
I can do encapsulation in non-OO programming. For example, I can create a private package-level cache of data, create random keys, and require that key to access the data for that "object" (abstract data type), rather than passing around a hash ref. I've created encapsulation.
What I can't do without OO is polymorphism. I cannot create a new ADT that has all of the functionality of the old ADT, except overriding some function to extend it. That requires inheritance.
As to MI - I don't avoid it. I embrace it as much as possible. The only thing is, that I've only found 1 time in about 10 years of OO programming where MI was the right answer. And that was 9 years ago. It's enough to convince me that languages without MI (such as Java) are seriously crippled for certain paradigms, although Java's MI (extends one class, implements many others) is sufficient for a lot of other paradigms.
Perl is somewhere between C++ and Java on MI. Many of perl's classes don't really have any useful constructor. Object types which don't need any construction, but can mix in code fragments (which is impossible in Java, but not C++) are often called "plugins" in Perl. This type of MI is incredibly useful in those problem spaces. And you may not have even realised you were using MI - many plugins "use base" on themselves for you (or equivalent). It's kind of an abuse on Exporter... but it works. And you can still choose to override these functions to extend them - giving you the polymorphism you need.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Understanding 'Multiple Inheritance'
by chromatic (Archbishop) on Mar 07, 2005 at 19:40 UTC | |
by Tanktalus (Canon) on Mar 07, 2005 at 21:04 UTC | |
by chromatic (Archbishop) on Mar 07, 2005 at 22:06 UTC | |
by tilly (Archbishop) on Mar 08, 2005 at 02:08 UTC | |
|
Re^3: Understanding 'Multiple Inheritance'
by Mugatu (Monk) on Mar 07, 2005 at 18:10 UTC | |
|
Re^3: Understanding 'Multiple Inheritance'
by tilly (Archbishop) on Mar 08, 2005 at 02:19 UTC |