Post #112,770
8/5/03 12:09:43 PM
8/21/07 6:36:01 AM
|
Not quite that simple
But it depends on the language.
For instance, C++ has private inheritance. This means "is implemented in terms of". For your logging thing privately inheriting loggability into stuff might make sense. Especially since your methods want access to the logging but you don't want clients playing with it.
In a dynamic language - inheritance is primarily used to share code - not so much for isa type stuff. Of course here delegation is simpler to implement. The rigidity of interfaces doesn't really exist - you need only implement as much interface as your client requires. Plus, its trivial to put an object in between and forward the messages to different objects. ie you can do something like
doesNotUnderstand: aMessage
delegates do: [:each | (each respondsTo: aMessage selector) ifTrue: [each perform: aMessage selector withArguments: aMessage arguments]]
and to be complete
respondsTo: aSelector
delegates do: [:each | (each respondsTo: aSelector) ifTrue: [^true]]. ^false
This gives you the functional equivalent of multiple inheritance using delegation.
I do notice that I use inheritance much less in the dynamic languages than in the static ones. Inheritance is practically the only tool you have in java. Delegation is a pain to implement if the interface is very large at all because you have to write the forwarding messages all yourself.
Smalltalk is dangerous. It is a drug. My advice to you would be don't try it; it could ruin your life. Once you take the time to learn it (to REALLY learn it) you will see that there is nothing out there (yet) to touch it. Of course, like all drugs, how dangerous it is depends on your character. It may be that once you've got to this stage you'll find it difficult (if not impossible) to "go back" to other languages and, if you are forced to, you might become an embittered character constantly muttering ascerbic comments under your breath. Who knows, you may even have to quit the software industry altogether because nothing else lives up to your new expectations. --AndyBower
|
Post #112,774
8/5/03 12:26:10 PM
|
Thing I love/hate about smalltalk
doesNotUnderstand: aMessage
delegates do: [:each | (each respondsTo: aMessage selector) ifTrue: [each perform: aMessage selector withArguments: aMessage arguments]]
and to be complete
respondsTo: aSelector
delegates do: [:each | (each respondsTo: aSelector) ifTrue: [^true]]. ^false I can never tell if it's pseudo-code or the real thing.
===
Implicitly condoning stupidity since 2001.
|
Post #112,777
8/5/03 12:41:34 PM
|
Similar to Python in that fashion
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #112,779
8/5/03 12:50:32 PM
8/21/07 6:36:13 AM
|
Its real
Smalltalk is dangerous. It is a drug. My advice to you would be don't try it; it could ruin your life. Once you take the time to learn it (to REALLY learn it) you will see that there is nothing out there (yet) to touch it. Of course, like all drugs, how dangerous it is depends on your character. It may be that once you've got to this stage you'll find it difficult (if not impossible) to "go back" to other languages and, if you are forced to, you might become an embittered character constantly muttering ascerbic comments under your breath. Who knows, you may even have to quit the software industry altogether because nothing else lives up to your new expectations. --AndyBower
|
Post #112,781
8/5/03 12:57:38 PM
8/21/07 6:36:18 AM
|
Forgot to mention: key driver is "substitutability"
isa implies I can provide one of these when it wants one of those.
ie - if the client expects a car - I can give it a Saturn, or a Chevy (a Ford wouldn't quite do I think :-P )
Smalltalk is dangerous. It is a drug. My advice to you would be don't try it; it could ruin your life. Once you take the time to learn it (to REALLY learn it) you will see that there is nothing out there (yet) to touch it. Of course, like all drugs, how dangerous it is depends on your character. It may be that once you've got to this stage you'll find it difficult (if not impossible) to "go back" to other languages and, if you are forced to, you might become an embittered character constantly muttering ascerbic comments under your breath. Who knows, you may even have to quit the software industry altogether because nothing else lives up to your new expectations. --AndyBower
|
Post #113,312
8/9/03 2:12:31 PM
|
Techies at Hertz
"I know you wanted a Cadillac, but polymorphism dictates that a Chevy Sprint is perfectly substitutable for one."
________________ oop.ismad.com
|
Post #113,327
8/9/03 3:53:19 PM
|
And if your Table or Data Structure is...
...not capable of handling a Cadillac, you're also out of luck.
|
Post #113,416
8/10/03 9:19:08 PM
8/21/07 12:45:06 PM
|
Mostly
"I know you wanted a Cadillac, but polymorphism dictates that a Chevy Sprint is perfectly substitutable for one." Sure, unless the client relies on openNorthStarNavigation - then things will go south fast. Of course, if the client does not rely on that feature, then the substitution is fine.
Smalltalk is dangerous. It is a drug. My advice to you would be don't try it; it could ruin your life. Once you take the time to learn it (to REALLY learn it) you will see that there is nothing out there (yet) to touch it. Of course, like all drugs, how dangerous it is depends on your character. It may be that once you've got to this stage you'll find it difficult (if not impossible) to "go back" to other languages and, if you are forced to, you might become an embittered character constantly muttering ascerbic comments under your breath. Who knows, you may even have to quit the software industry altogether because nothing else lives up to your new expectations. --AndyBower
|
Post #113,424
8/11/03 12:03:54 AM
|
Or perhaps that ....
MaxCarCapacity which returns 4 (maybe) for the Sprint and 6 for the Cadillac.
Ask the party of 6 driving 2 hours from Tulsa to Bentonville which object they want...
|
Post #112,805
8/5/03 4:42:32 PM
|
Code sharing vs. polymorphism
In a dynamic language - inheritance is primarily used to share code - not so much for isa type stuff. Of course here delegation is simpler to implement. The rigidity of interfaces doesn't really exist - you need only implement as much interface as your client requires...I do notice that I use inheritance much less in the dynamic languages than in the static ones. Inheritance is practically the only tool you have in java. Delegation is a pain to implement if the interface is very large at all because you have to write the forwarding messages all yourself.
I think you hit the nail on the head here. Inheritance should be about code sharing, but in a static language it's also how polymorphism is implemented. That overloading of inheritance makes it more difficult to for a beginner to design classes properly. I think the trick in Java is to stick with interfaces for polymorphism and use inheritance only for code sharing.
Plus, its trivial to put an object in between and forward the messages to different objects. ie you can do something like...
It's interesting how the next big thing in Java is AOP while along you could do the same thing in Smalltalk with "doesNotUnderstand".
Regards, John
|