John Williams wrote: > Reaction #2: Inheritance would automatically delegate all those > methods, so again, in what way does inheritance _not_ solve the problem?
Many real life systems are composed from elements, not inherited from elements. A car is not a wheel, but is composed from 4 (or more). As a simple example, a wheel might implement an inflate() method, but it probably wouldn't make much sense for the car to inherit that method. Rather, you would define inflate_tyres() which delegates to the inflate() method on the tyres on each of the the 4 wheels. Further still, the airbag might also have an inflate() method. If you're not careful and don't inherit all your objects in exactly the right order then you might find your tyres inflating instead of your airbag when you hit a truck. False inheritance leads to method madness. > In what way is an interface different from a pure abstract class (i.e. > containing only method declarations, but no code)? Inheritance and interfaces are two different things. The end result would be pretty much the same in this example, but reaching it by different routes. With inheritance, your derived object inherits all the methods that you don't explicity re-define. With interfaces, you are stating that your object will implement all the methods, either directly or by inheriting from, or delegating to other classes. Thus, inheritance, delegation and interfaces are separate, orthogonal concepts. Inheritance : is Delegation : has Interface : can A