Re: Multiple inheritance examples?



Pascal Costanza wrote:
Tayssir John Gabbour wrote:
Hi!

I'm helping a Java-influenced person learn CLOS concepts. Does anyone
have good examples of multiple inheritance? Ones which aren't too
abstract?

Java programmers actually use multiple inheritance more often than they are aware of. It's just more clumsy.

Here are some links:
- http://mindprod.com/jgloss/interfacevsabstract.html
- http://c2.com/ppr/wiki/JavaIdioms/BuildInterfaceImplementationPairs.html
- http://citeseer.ist.psu.edu/mohnen02interfaces.html

I know there was a better link for this idea, but I currently don't find it...

Basically, there is an idiom which is used in the Swing framework, but also elsewhere, where an API is presented in three entities: An interface that defines the API as such, an abstract class implementing that interface which provides template methods for easy subclassing, and a default implementation derived from that abstract class that can just be instantiated to get the behavior you 'usually' want.

If you subclass the abstract class, you automatically implement the interface, but get the convenience of useful default implementations for some aspects of the interface. If you need to be able to subclass a different class, you can still implement the interface, but have to implement all elements of the interface yourself (for example, by forwarding to an instance of the default implementation).

The latter corresponds to a technique sometimes called sibling classes, which is a way to manually simulate multiple inheritance. (Forwarding can have its own problems, because after forwarding, the 'this' reference has changed, so in the most general case you need some form of prototype-style delegation to fully simulate multiple inheritance.)

Sibling classes should exist quite often in large Java code bases. Look for those (and the above idiom of separating APIs into interface, abstract classe and default concrete classes), and you will have good examples for multiple inheritance.

Multiple inheritance is typically rejected because of potential conflicts between overriding methods. This largely doesn't exist in CLOS (although there are some minor remaining cases). However, in languages like C++ and Eiffel they do exist. This is mostly because in those languages, classes are also used to create namespaces, and you then inevitably get name clashes. CLOS doesn't have this problem because the handling of namespaces is handed out to the package system.

Since a few years, traits were developed (mostly in the Smalltalk community, but picked up elsewhere as well, for example in Fortress), which separates classes from mixins and thus gives you some way of better controlling such conflicts. It's essentially a variation on "interfaces with default implementations", which are copied into implementing classes unless they provide their own definitions. Traits seem to make multiple inheritance somewhat more palatable to a general audience, but the problems they solve are not pressing enough IMHO. Traits also still seem to have some problems with multiple dispatch...


Pascal

--
1st European Lisp Symposium (ELS'08)
http://prog.vub.ac.be/~pcostanza/els08/

My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.



Relevant Pages

  • Re: "Must instantiate controlled types at library level." Why?
    ... You will have this for free with multiple inheritance. ... You cannot have objects of an abstract type (interface). ... Is it impossible to have a generic function that operates on ... we could create such supertypes as necessary *afterwards*. ...
    (comp.lang.ada)
  • Re: Interfaces and abstract classes et al
    ... One very simple use of interfaces over an abstract class is when wanting to ... 'inherit' from multiple base classes. ... multiple interfaces to provide the multiple inheritance functionality. ... usefulness of an interface over an abstract class. ...
    (microsoft.public.dotnet.csharp.general)
  • Re: Multiple Inheritance
    ... Then, to me, it can be read "Multiple inheritance, as done in C++, ... was discarded from Java". ... that multiple interface inheritance is not multiple inheritance. ... showed that your quoting of "Gosling" (if he really did write ...
    (comp.lang.java.programmer)
  • Re: ruby abstraction
    ... same interface, but not multiple inheritance. ... It seems like this exact problem exists just as much in an interface, ... fact that Java makes setters and getters ridiculously verbose. ... abstract methods, but which can be multiply inherited from. ...
    (comp.lang.ruby)
  • !Re: Why cant C# or Java support multiple inheritance? But inherit multiple interfaces instead??
    ... that's what the Java folks did. ... Inspiration for the Interface ... In Java you included multiple inheritance of interface, ...
    (comp.object)