Quantcast
Channel: Answers by "dubbreak"
Viewing all articles
Browse latest Browse all 68

Answer by dubbreak

$
0
0
In my opinion. No. I don't see a clear relationship. It's not a good use of polymorphism. Using inheritance for the sake of reusing a few methods that might be the same (so you can use polymorphism) is a perfect example of when not to use it. Inheritance should only be used in clases with a clear is-a realtionship. E.g. an apple is a fruit (very simple) or recently I had a black jack hand which extends a card hand (collection of cards belonging to a player) that has special methods added to it that are black jack only (such as the hand value). It was clear that is was a sub case of the general case and the base case had other legitimate ways to extend it(e.g. poker hand). Using inheritance for the sake of polymorphism alone is an anti pattern and an abuse of OOP. I worked with someone that did that, and his stuff was not maintainable. You should look at using an interface or some kind of class composition rather than inheritance if you are worried about code duplication. Here is a good [stackoverflow post on composition vs inheritance][1]. Inheritance can result in some pretty nasty lock in that's difficult to refactor out. This becomes especially apparent when it gets miss-used. The end uses aren't actually related so you end up with them diverging and the base class not actually working for either very well. I remember a base communication class that should have been an interface (to guarantee each communication type had the agreed on interface to get and send data). Instead is was a base class that locked in certain behavior and created some horrible coupling that was very difficult to fix. [1]: http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance

Viewing all articles
Browse latest Browse all 68

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>