3 Replies Latest reply on Oct 2, 2006 5:45 PM by 807607

    Polymorphism or not?

      There is something that I'm not sure about regarding polymorphism. If there is no polymorphic method to be inherited by derived classes, is polymorphism still useful? I have an application where I cannot find any common method to all elements, yet they have something in common and having them deriving from a base class could have the benefit of storing all of them in a single array of base class references. Do you think this is useful?
        • 1. Re: Polymorphism or not?
          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
          • 2. Re: Polymorphism or not?
            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.

            • 3. Re: Polymorphism or not?
              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.