This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
An extremist position on inheritance
Posted by Terry Braun on 21 Oct 1998, 2:10 PM
I have been thinking about this quite a bit. I would take the requirement about "is-a" further however. If we use the example of "vehicles", "cars" and "trucks" instead of fruit the reasoning may be clearer. Clearly, a car is-a vehicle, as is a truck, and therefore should use inheritance. But there are two ways this breaks down. What exactly is a car? When you have a cargo space is that a car? Like a van? Like a Ford Explorer? Is a truck with a camper a truck or a car? What about a motorcycle, an ATV, a bulldozer, and so on. You may think this is a silly example, but I was part of a two hour long discussion once about what a computer "device" is. This discussion was exactly along these line. What if you emulate the device in software, is it a device? What if it sometimes is emulated and sometimes is hardware assisted? I don't really know what to call this problem. It seems to me that when designing classes ( or the model on which to base classes) that sometimes there is a tendency to classify things. The second problem I have with inheritance is that many things are better modeled and programmed as systems with components. Over time the program that uses car and truck classes will tend to expand to the point that we are interested in more information about vehicles. What type of engine does it have? Does it have a trailer? The single object car, then breaks down as you need to expand it into an aggregation of components. If you use composition from the start then the conversion to using components is easy. At one point the rather extreme saying "inheritance is the goto of OO" popped into my mind. I don't think this is really true, but I have been trying to think of when it is good to use inheritance (since I disagree with the "is-a" test). I also read the article about the java event idiom, and that lead me to one circumstance- as a parameterized instance of a class. As you can tell my thinking on this has a fair amount of fog and I would appreciate seeing more discussion.
Replies:
|