3 ms·
The classic examples... Person > Employee > Manager Shape > Rectangle, Circle Object > anything Product > Book, TV I think these are good examples where in
by sytelus 11y ago
The classic examples...
Person > Employee > Manager
Shape > Rectangle, Circle
Object > anything
Product > Book, TV
I think these are good examples where inheritance makes lot of sense. There is commonality between things and there is specialization (overrides) or definitions (abstract methods). It's wrong to say that the concept of inheritance is evil. Like pretty much everything, it can be severely abused (very deep hierarchies, multiple on inheritence etc). The classic example of OOP abuse is probably things like MFC library.
- thomasahle 11y agoWould you also support Shape > Rectangle > Square? Or are all your super classes abstract?
- Bluestrike2 11y agoIf we're being cheeky, a square is also a rectangle. Unless I'm misreading your post, I'd expect your superclasses to be as abstract as they needs to be and no more: Vehicle < Car, Truck, etc. Person < Employee, Customer, etc. There's a pretty obvious logic to them that doesn't really demand much extra thought. I can't think of anyone outside of an irreverent ontologist who would suggest going beyond that unless there's a valid reason for it, like say: PhilosophicalBeing < Person < Employee, Customer, etc.
- tamana 11y agoA mutable square is not a mutable rectangle, because a mutablr rectangle can change its width independently of its length. This is the classic problem of contravariance: subtyping in the face of mutability.
- Bluestrike2 11y agoGood point. Without giving it much if any thought, I made an assumption--that the square is immutable--that probably wouldn't even be accurate in this context. In fact, it's the usual example used to talk about substitutability. Oops.
- deleted 11y ago[deleted]
- deleted 11y ago[deleted]
- lbarrow 11y agoIt's hard to evaluate whether or not those examples make sense without some pseudocode showing how the classes are used.
- yxhuvud 11y agoI would disagree with regards to the Person chain. Mainly because being an employee is a role, as opposed to being a kind of people.