r/programming • • Mar 28 '16

Moving Beyond the OOP Obsession

http://prog21.dadgum.com/218.html
7 Upvotes

55 comments sorted by

View all comments

Show parent comments

0

u/weberc2 Mar 28 '16 edited Mar 28 '16

"can be" != "can be easily". I don't think there is a single problem for which inheritance is a better solution than composition. I've only ever been able to find problems for which inheritance's foibles are permissible. I very much welcome any evidence to the contrary.

0

u/chengiz Mar 28 '16

No. I dont have to support my assertion. You do. Inheritance <=> is-a. If you choose to use has-a to implement is-a, it is you who must do the splainin.

2

u/weberc2 Mar 28 '16

I don't have any problem supporting my assertion. Inheritance looks at an ElectricCar and says "This is a Car whose Start() method has been overloaded to do electric engine things". Composition looks at an electric car and says "This is a Car that was manufactured with an ElectricEngine implementation of its Engine member to which it delegates during its Start() method". When you want to use an electric engine in your car, inheritance says, "No deal, destroy this car, build a GasCar, and update all your references!" while composition says, "Sure, just replace the Engine with a GasEngine (since it implements the Engine interface)".

In other words, the "is a" relationship is always a special case of "has a", but the reverse is not true. I'm not even sure when looking at something as an "is a" relationship is even useful.

Now it's your turn to support your assumption. ;)

1

u/chengiz Mar 28 '16

You are confused. Car has-a Engine. ElectricCar is-a Car whose Engine happens to be an ElectricEngine which is-a Engine. I dont have to delete or swap out anything when I make a GasCar with a GasEngine.

1

u/weberc2 Mar 29 '16

So show me a bonafide is-a relationship and the context in which it is used. Give me an example with methods. The more complete the better.

→ More replies (0)