Home > Archive > Smalltalk > February 2006 > Multiple Inheritance in Smalltalk
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Multiple Inheritance in Smalltalk
|
|
| Leonid Shchervinsky 2006-01-30, 7:04 pm |
| In Java there are interfaces. How does Smalltalk simulate multiple
inheritance?
| |
| jarober@gmail.com 2006-01-30, 7:04 pm |
| Polymorphism. Any object can implement any interface it wants to. For
instance, if you wanted any object to be able to use some of the
Collection protocol, you could add this (and other) methods to class
Object:
select: selectBlock
^(Array with: self) select: selectBlock
| |
|
|
| Peter van Rooijen 2006-01-30, 7:04 pm |
| jarober@gmail.com wrote:
> Polymorphism. Any object can implement any interface it wants to. For
> instance, if you wanted any object to be able to use some of the
> Collection protocol, you could add this (and other) methods to class
> Object:
>
> select: selectBlock
> ^(Array with: self) select: selectBlock
Hi Jim!
Yes, you can do this.
You would probably, if you're going to do such a general thing, want to
code it differently:
select: selectBlock
^(selectBlock value: self)
ifTrue: [Array with: self]
ifFalse: [#()]
but then, you would be tempted to treat nil as equivalent to what?
let's say you think of it as equivalent to the empty collection #()
so then you'd equip UndefinedObject with:
select: selectBlock
^#()
but then you might be tempted to go all the way and instead do:
UndefinedObject>>select: selectBlock
^self "or: ^nil"
but then if you did that, you could (should?) make your original
Object>>select: selectBlock
^(selectBlock value: self)
ifTrue: [Array with: self]
and you'd be pleased with yourself, thinking you had made general objects
behave as collections of one object, and pretty soon you'd be sending
#size to (3@4), getting 0 instead of the expected 1
(which you would get from ((3@4) select: [:x | true]) size).
and you'd be sending #size to nil and getting a real error.
and maybe then you'd come to the conclusion that while it may look like
"Any object can implement any interface it wants to", this is not in
reality the case.
In Smalltalk, the only interfaces that exist that have significant
similarity to the concept of interface you are talking about, are all
the degenerate "interfaces" that exist of exactly 1 selector. And of
those an object can implement only those that it has room for.
Of course there is a way in which what you said is true, and it can be a
very practical/useful/powerful way. But it isn't true in general. You've
overstated your case.
Cheers,
Peter
P.S. none of this code tested. likely contains bugs.
| |
|
| Here is an example of what I am trying to do:
Boat and Car classes have some characteristics in common. So they inherit
them from and abstract class Vehicle.
Now I am designing a new class Amphibian which has characteristics of both
Boat and Car. How would you design Amphibian class hierarchy?
"Leonid Shchervinsky" <leonid@shchervinsky.com> wrote in message
news:3grDf.8050$rH5.5406@newsread2.news.atl.earthlink.net...
> In Java there are interfaces. How does Smalltalk simulate multiple
> inheritance?
>
| |
| Hans-Martin Mosner 2006-01-30, 7:04 pm |
| "LSS" <leonid@shchervinsky.com> wrote:
> Here is an example of what I am trying to do:
>
> Boat and Car classes have some characteristics in common. So they inherit
> them from and abstract class Vehicle.
>
> Now I am designing a new class Amphibian which has characteristics of both
> Boat and Car. How would you design Amphibian class hierarchy?
That's a classical misunderstanding - there's more to modeling than inheritance.
In reality, a new car model does not inherit from some abstract car
model - it is designed using a mixture of copy-and-paste from old
designs and newly invented material. The closest thing to this process
in Smalltalk is that of composition, not of inheritance. Of course, you
don't build vehicles in Smalltalk, but you build computer models of
real-life things and of things which only make sense with in the context
of computers.
If we stay with real-life things, you could compose vehicles out of a
number of components:
- engine
- hull
- method of traction
- passenger compartment
- cargo compartment
A car would have maybe a Diesel engine, a normal steel sheet hull,
tires, a passenger compartment with a number of seats and a trunk for suitcases.
A (small) boat will maybe have a two-stroke motor and a propeller, a
wooden or steel watertight body, etc.
For an amphibian vehicle, you would have to combine components which are
needed for operation on land and water: tires, propeller, a watertight
body, etc.
(Multiple) Inheritance won't help you much here.
You can implement composition in different ways. The classical method is
that of delegation: Having instance variables for the different
components, and delegating the protocols related to their function to
the object in those variables.
Another possibility which is currently being implemented in some
Smalltalk dialects is to use traits. A trait is a collection of methods
which are not bound to a specific class but can be added to classes to
extend their behavior.
In your amphibian example, the first method is probably more appropriate
because it allows the object to operate in different modes depending on
its environment: When in water, the motor would be attached to the
propeller instead of the wheels.
Cheers,
Hans-Martin
| |
| David Buck 2006-01-30, 9:57 pm |
| Three answers:
1) Don't do it that way
2) If you do it that way, it still works
3) There's a new mechanism that can help
In more detail.
Answer 1:
Mixing and matching subclasses is a code smell that your design isn't
very good. It would be better from a design standpoint to have a
Vehicle class that delegates to helper classes to handle the details.
In this case, it's hard to talk about an artificial model where I don't
know the purpose.
Answer 2:
Even if you wanted to have a Vehicle class with Boat and Car subclasses
then create an Amphibian class, you can get it to work. Choose either
Boat or Car as the superclass of Amphibian. Implement the missing
methods in Amphibian.
Smalltalk does message dispatching by name, not by number. In Java, the
message-send bytecodes would say "send message #3". In Smalltalk, it
would say "send the message select:". In Smalltalk, we use the name of
the method, not its index in the method table. Because of this, two
objects from completely different class hierarchies can be used
interchangeably so long as they implement the appropriate methods. If
an object doesn't implement a method, it's detected as a run-time error,
not a compile-time error.
Answer 3:
If you are concerned about the fact that Answer 2 requires you to
re-implement methods with identical code in two different classes,
there's a newer mechanism called Traits that allows you to share the
code even though you have different classes. It's kind of like mixins
in C++.
Hope this helps
David Buck
Simberon Inc.
www.simberon.com
LSS wrote:[color=darkred]
> Here is an example of what I am trying to do:
>
> Boat and Car classes have some characteristics in common. So they inherit
> them from and abstract class Vehicle.
>
> Now I am designing a new class Amphibian which has characteristics of both
> Boat and Car. How would you design Amphibian class hierarchy?
> "Leonid Shchervinsky" <leonid@shchervinsky.com> wrote in message
> news:3grDf.8050$rH5.5406@newsread2.news.atl.earthlink.net...
>
| |
| Chris Uppal 2006-01-31, 7:59 am |
| Leonid Shchervinsky wrote:
> In Java there are interfaces. How does Smalltalk simulate multiple
> inheritance?
Incidentally, Java interfaces are nothing to do with "simulating multiple
inheritance". Java's interfaces are a way of assuring the typechecker that a
given object provides a certain set of operations. It's using the typechecker
to enforce a contract. This has nothing whatever to do with
multiple-inheritance as I think you mean it. In Smalltalk, the approach to
typeing is completely different, and there is no up-front attempt to police
contracts. The effect is that any object which actually /does/ provide a given
set of operations is valid in any context that requires those operations. You
could say that every Smalltalk objects implements every interface it could
possibly could.
So, in the sense in which Java has multiple inheritance, Smalltalk has exactly
the same degree of multiple inheritance (but packaged differently, and more
flexibly).
But I suspect that you mean inheritance of implementation -- an unrelated
concept. Other respondents have already talked about that.
-- chris
| |
| Mike Anderson 2006-01-31, 7:07 pm |
| LSS wrote:[color=darkred]
> Here is an example of what I am trying to do:
>
> Boat and Car classes have some characteristics in common. So they inherit
> them from and abstract class Vehicle.
>
> Now I am designing a new class Amphibian which has characteristics of both
> Boat and Car. How would you design Amphibian class hierarchy?
> "Leonid Shchervinsky" <leonid@shchervinsky.com> wrote in message
> news:3grDf.8050$rH5.5406@newsread2.news.atl.earthlink.net...
I think people are because, in your second post, you are
talking about class inheritance, but in your first post you are talking
about interfaces.
Java and Smalltalk both have single inheritance, so you would design
your class hierarchy in exactly the same way in both.
You would also design your interfaces in exactly the same was as in
Java; but you don't declare them in Smalltalk, just implement them.
Mike
| |
| Vlastimil Adamovsky 2006-02-11, 7:01 pm |
| Interfaces does not simulate multiple inheritance.
Of cource, you can have interfaces in Smalltalk, which can be inherited by
other interfaces...
In Smalltalk, I think proxies is the way to go...
"Leonid Shchervinsky" <leonid@shchervinsky.com> wrote in message
news:3grDf.8050$rH5.5406@newsread2.news.atl.earthlink.net...
> In Java there are interfaces. How does Smalltalk simulate multiple
> inheritance?
>
>
| |
| Vlastimil Adamovsky 2006-02-11, 7:01 pm |
| Avoid implementation ingeritance whenever possible
"Leonid Shchervinsky" <leonid@shchervinsky.com> wrote in message
news:3grDf.8050$rH5.5406@newsread2.news.atl.earthlink.net...
> In Java there are interfaces. How does Smalltalk simulate multiple
> inheritance?
>
>
|
|
|
|
|