This content has been marked as final. Show 3 replies
inheritance though plays a very important part in polymorphism but reusability of code is not the reason you would use inheritance.
Inheritance is governed by other laws like "Liskov's Principle".
I would say in more than 80% of the cases where inheritance was used the developer would have been better of using aggregation or composition. HTH
I tend to be rather strict with my use of Inheritance. Inheriting from a base class simply to inherit some common data fields is often unclear. I'm much more likely to create a base class to encapsulate even a single common method than to cause all base classes to inherit a set of data fields. In my mind, common behaviour defines ancestors much better than common data.
if you have common data, you probably have a common behavior for storing and retrieving it. But in reallity, what you are asking whether or not you should use inheritance. Polymorphism is a (side) affect introducted by inheritance and abstraction, as such you don't get to choose whether to have it or not. As far as inheritance goes, you may need or or may not. You don't provide enough details about the specifics of your domain to make that call. Generally, I'd say, stay away from inheritance because it introduced tight coupling into the design. Replace it by composition or aggregation whenever possible.