Multiple Inheritance
This is a very general question about mulitple inheritance.
I now how it's done (or perhapsnot done) in Java, but I'd like to know how it's done in general.
I can understand an amphibious vehicle inheriting the ability to drive from Car and the ability to navigate from Boat, but how does one deal with conflicting properties?
For instance, take Garfield. He is a Cat, and therefore unable to speak - he meows. But he's also a CartoonCharacter, and because of that hedoes speak. So how do you resolve this?
Can anyone explain?
[577 byte] By [
SQBa] at [2007-9-28 2:46:06]

Well, it rather is vey simple. Garfield is indeed a cat, so he cant speak and only meows.
Garfield may inherit from Cat as well as from the CartoonCharacter.
Now, depending on whether you want Garfield to meow, talk, do both or do none, you could inherit these qualities in different ways (by making them private/public/protected etc). It is not possible in Java to perform multiple inheritance but in C++ it is done quite commonly.
For instance, if you want Garfield to do both - meow and talk(provided both the properties and declared at least protected in either of the classes) you could inherit from both the classes directly.
Now, if you do not want Garfield to meow or talk, you will have to make the two properties private in both the classes. You could do various combinations of them also.
However, OO is a logical animal - people may arm twist it to do things but logical progression is the best course of action. So, since Garfield is both, a cat as well as a CartoonCharacter, logically he should be able to meow and talk (the inherent property of both the classes). So, let Garfield meow and talk at his will - logical right!
Ironluca
Okay, I understand that, and maybe Garfield wasn't the perfect example. But still the question remains: what if a Class inherits two conflicting properties from two different SuperClasses - how do you determine which property gets cancelled?
You say that I'll have to decide whether to make the properties private, to decide that.
But that seems illogical. Because other Cats will need to inherit meowing and other CartoonCharacters (Jon, for instance) will need to inherit talking.
Also, deciding what a SubClass inherits from which SuperClasses, means that at the time you create the SubClass, you need to know what properties are available and whether they will clash. And, when you add properties to a SuperClass, you need to find out whether they will clash somewhere in a SubClass.
So I still need to know: how do you do it? Is there a specific method or algorithm for it? Are there any rules?
SQBa at 2007-7-7 22:19:08 >

This depends on the specification of the programming language you use.
There is a quite compact approach to this subject under http://www.elj.com/eiffel/feature/inheritance/mi/review/ in chapter 4.
If you want to use multiple inheritance in java (you can't) try delegation, as described in chapter 5. For your Garfield example I would suggest delegating the the meowing to the Cat class (public static void Cat.meow()).
K鰉
koema at 2007-7-7 22:19:08 >

you are correct, the Forbin project has yet to surface in the world of object-oriented programming. computer languages are not yet smart enough to develop logic where instructions or protocol or rules do not exist.
however, in the example you gave, perhaps you might want to create a interface class (in Java) or a third class (in C++), let's say talkClass as an example.
talkClass would contain properties and methods representing one or many events (talking slow, talking fast, not talking, temporarily mute, etc.).
your animal objects could implement talkClass and override methods of talkClass (in Java) or inherit and implement in C++.
as ironluca stated use logic (what are you trying to accomplish).
just a thought.
by the way snoopy is a dog and cartooncharacter but i do not believe he can talk as well as bark. however, he can create thought clouds.
Well, as I said, the Garfield example may have been a bad example. But I still don't know what happens, regardless of which computer language was used, when in any Multiple Inheriting System (MIS for short) a class inherites two conflicting properties, for any reason, even if that reason happens to be a design error.
Take for instance X. X isa Gadget. A Gadget has a VolumeController. But X isa Thing too. A Thing has no VolumeController.
Of course an error in design was made here, but regardless of that:
- Does X have a VolumeController?
- Is there any way to determine what happens?
Thanks for your answers so far.
SQBa at 2007-7-7 22:19:08 >

It's up to the language, there is no general aproach.
From the language point of view two properties can't contradict each other, although their semantics might.
The biggest problem you can face from the language point of view are ambiguaties like this one
class A has a property a
B and C inherit from A
D inherits from B and C
How is the property a defined for B and C ... what ever you do it ends up being confusing.
Back to the contradicting properties.
What you are refering to I assume are properties are supposed not to exist. Semantically a cat might have the property of not being able to meow, but this is a semantic not defined in the language, so there is no need for the language to solve it.
The proper solution for the user of such a language would be not to use multiple inheritance in such a case
regards
Spieler
> It's up to the language, there is no general aproach.
>
> From the language point of view two properties can't
> contradict each other, although their semantics
> might.
>
> The biggest problem you can face from the language
> point of view are ambiguaties like this one
>
> class A has a property a
> B and C inherit from A
> D inherits from B and C
>
> How is the property a defined for B and C ... what
> ever you do it ends up being confusing.
>
> Back to the contradicting properties.
> What you are refering to I assume are properties are
> supposed not to exist. Semantically a cat might have
> the property of not being able to meow, but this is a
> semantic not defined in the language, so there is no
> need for the language to solve it.
>
> The proper solution for the user of such a language
> would be not to use multiple inheritance in such a
> case
>
> regards
> Spieler
Again there are some laid down principles for inheritance as well. Refer to Liskov's principal and why to use it that explains a lot. For according to some of the experts more than 80% of the time when inheritance is used it is not even legal to use it over there let alone mutiple inheritance. REFER TO LISKOV'S PRINCIPAL.
