<p>In a project our team is using object lists to perform mass operations on sets of data that should all be processed in a similar way.  In particular, different objects would ideally act the same, which would be very easily achieved with polymorphism.  The problem I have with it is that inheritance implies the <em>is a</em> relationship, rather than the <em>has a</em> relationship.  For example, several objects <em>have a</em> damage counter, but to make this easy to use in an object list, polymorphism could be used - except that would imply an <em>is a</em> relationship which wouldn't be true.  (A person <em>is not a</em> damage counter.)</p>

<p>The only solution I can think of is to have a member of the class return the proper object type when implicitly casted instead of relying on inheritance.  Would it be better to forgo the <em>is a</em> / <em>has a</em> ideal in exchange for ease of programming?</p>

<p>Edit:
To be more specific, I am using C++, so using polymorphism would allow the different objects to "act the same" in the sense that the derived classes could reside within a single list and be operated upon by a virtual function of the base class.  The use of an interface (or imitating them via inheritance) seems like a solution I would be willing to use.</p>

[{'answer_id': 11859, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': '<p>Sometimes it\'s worth giving up the ideal for the realistic.  If it\'s going to cause a massive problem to "do it right" with no real benefit, then I would do it wrong.  With that said, I often think it\'s worth taking the time to do it right, because unnecessary multiple inheritance increases complexity, and it <em>can</em> contribute to the system being less maintainable.  You really have to decide what\'s best for your circumstance.</p>\n\n<p>One option would be to have these objects implement a <code>Damageable</code> interface, rather than inheriting from <code>DamageCounter</code>.  This way, a person <em>has-a</em> damage counter, but <em>is</em> damageable.  (I often find interfaces make a lot more sense as adjective than nouns.)  Then you could have a consistent damage interface on <code>Damageable</code> objects, and not expose that a damage counter is the underlying implementation (unless you need to).</p>\n\n<p>If you want to go the template route (assuming C++ or similar), you could do this with mixins, but that can get ugly really quickly if done poorly.</p>\n'}, {'answer_id': 11860, 'author': 'Kev', 'author_id': 419, 'author_profile': 'https://Stackoverflow.com/users/419', 'pm_score': 0, 'selected': False, 'text': '<p>Normally when we talk about \'is a\' vs \'has a\' we\'re talking about Inheritance vs Composition.</p>\n\n<p>Um...damage counter would just be attribute of one of your derived classes and wouldn\'t really be discussed in terms of \'A person is a damage counter\' with respect to your question.</p>\n\n<p>See this:</p>\n\n<p><a href="http://www.artima.com/designtechniques/compoinh.html" rel="nofollow noreferrer">http://www.artima.com/designtechniques/compoinh.html</a></p>\n\n<p>Which might help you along the way.</p>\n\n<p>@<strong><a href="https://stackoverflow.com/questions/11854/inheritance-and-polymorphism-ease-of-use-vs-purity#11864">Derek</a></strong>: From the wording, I assumed there <em>was</em> a base clase, having re-read the question I kinda now see what he\'s getting at. </p>\n'}, {'answer_id': 11862, 'author': 'Andrew Grant', 'author_id': 1043, 'author_profile': 'https://Stackoverflow.com/users/1043', 'pm_score': 0, 'selected': False, 'text': '<p>This question is really confusing :/</p>\n\n<p>Your question in bold is very open-ended and has an answer of "it depends", but your example doesn\'t really give much information about the context from which you are asking. These lines confuse me;</p>\n\n<blockquote>\n  <blockquote>\n    <p>sets of data that should all be processed in a similar way</p>\n  </blockquote>\n</blockquote>\n\n<p>What way? Are the sets processed by a function? Another class? Via a virtual function on the data?</p>\n\n<blockquote>\n  <blockquote>\n    <p>In particular, different objects would ideally act the same, which would be very easily achieved with polymorphism</p>\n  </blockquote>\n</blockquote>\n\n<p>The ideal of "acting the same" and polymorphism are absolutely unrelated. How does polymorphism make it easy to achieve?</p>\n'}, {'answer_id': 11864, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': "<p>@Kevin</p>\n<blockquote>\n<p>Normally when we talk about 'is a' vs 'has a' we're talking about Inheritance vs Composition.</p>\n<p>Um...damage counter would just be attribute of one of your derived classes and wouldn't really be discussed in terms of 'A person is a damage counter' with respect to your question.</p>\n</blockquote>\n<p>Having the damage counter as an attribute doesn't allow him to diverse objects with damage counters into a collection.  For example, a person and a car might both have damage counters, but you can't have a <code>vector&lt;Person|Car&gt;</code> or a <code>vector&lt;with::getDamage()&gt;</code> or anything similar in most languages.  If you have a common Object base class, then you can shove them in that way, but then you can't access the <code>getDamage()</code> method generically.</p>\n<p>That was the essence of his question, as I read it.  &quot;Should I violate <code>is-a</code> and <code>has-a</code> for the sake of treating certain objects as if they are the same, even though they aren't?&quot;</p>\n"}, {'answer_id': 11868, 'author': 'Andrew', 'author_id': 1389, 'author_profile': 'https://Stackoverflow.com/users/1389', 'pm_score': 0, 'selected': False, 'text': '<p>"Doing it right" will have benefits in the long run, if only because someone maintaining the system later will find it easier to comprehend if it was done right to begin with.</p>\n\n<p>Depending on the language, you may well have the option of multiple inheritance, but normally simple interfaces make the most sense.  By "simple" I mean make an interface that isn\'t trying to be too much.  Better to have lots of simple interfaces and a few monolithic ones.  Of course, there is always a trade off, and too many interfaces would probably lead to ones being "forgotten" about...</p>\n'}, {'answer_id': 11869, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 0, 'selected': False, 'text': '<p>@Andrew</p>\n\n<blockquote>\n  <p>The ideal of "acting the same" and polymorphism are absolutely unrelated. How does polymorphism make it easy to achieve?</p>\n</blockquote>\n\n<p>They all have, e.g., one function in common.  Let\'s call it <code>addDamage()</code>.  If you want to do something like this:</p>\n\n<pre><code>foreach (obj in mylist)\n    obj.addDamage(1)\n</code></pre>\n\n<p>Then you need either a dynamic language, or you need them to extend from a common parent class (or interface).  e.g.:</p>\n\n<pre><code>class Person : DamageCounter {}\nclass Car : DamageCounter {}\n\nforeach (DamageCounter d in mylist)\n    d.addDamage(1)\n</code></pre>\n\n<p>Then, you can treat <code>Person</code> and <code>Car</code> the same in certain very useful circumstances.</p>\n'}, {'answer_id': 11881, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 3, 'selected': False, 'text': "<p>I think you should be implementing interfaces to be able to enforce your <em>has a</em> relationships (am doing this in C#):</p>\n\n<pre><code>public interface IDamageable\n{\n    void AddDamage(int i);\n    int DamageCount {get;}\n}\n</code></pre>\n\n<p>You could implement this in your objects:</p>\n\n<pre><code>public class Person : IDamageable\n\npublic class House : IDamageable\n</code></pre>\n\n<p>And you'd be sure that the DamageCount property and has a method to allow you to add damage, without implying that a person and a house are related to each other in some sort of heirarchy.</p>\n"}, {'answer_id': 11935, 'author': 'Brian', 'author_id': 725, 'author_profile': 'https://Stackoverflow.com/users/725', 'pm_score': 3, 'selected': True, 'text': "<p>This can be accomplished using multiple inheritance.  In your specific case (C++), you can use pure virtual classes as interfaces.  This allows you to have multiple inheritance without creating scope/ambiguity problems.  Example:</p>\n\n<pre><code>class Damage {\n    virtual void addDamage(int d) = 0;\n    virtual int getDamage() = 0;\n};\n\nclass Person : public virtual Damage {\n    void addDamage(int d) {\n        // ...\n        damage += d * 2;\n    }\n\n    int getDamage() {\n        return damage;\n    }\n};\n\nclass Car : public virtual Damage {\n    void addDamage(int d) {\n        // ...\n        damage += d;\n    }\n\n    int getDamage() {\n        return damage;\n    }\n};\n</code></pre>\n\n<p>Now both Person and Car 'is-a' Damage, meaning, they implement the Damage interface.  The use of pure virtual classes (so that they are like interfaces) is key and should be used frequently.  It insulates future changes from altering the entire system.  Read up on the Open-Closed Principle for more information.</p>\n"}, {'answer_id': 70856, 'author': '0124816', 'author_id': 11521, 'author_profile': 'https://Stackoverflow.com/users/11521', 'pm_score': 1, 'selected': False, 'text': '<p>I agree with Jon, but assuming you still have need for a separate damage counter class, you can do:</p>\n\n<pre><code>class IDamageable {\n  virtual DamageCounter* damage_counter() = 0;\n};\nclass DamageCounter {\n  ...\n};\n</code></pre>\n\n<p>Each damageable class then needs to provide their own damage_counter() member function. The downside of this is that it creates a vtable for each damageable class. You can instead use:</p>\n\n<pre><code>class Damageable {\n public:\n  DamageCounter damage_counter() { return damage_counter_; }\n private:\n  DamageCounter damage_counter_;\n};\n</code></pre>\n\n<p>But many people are <strong>Not Cool</strong> with multiple inheritance when multiple parents have member variables.</p>\n'}, {'answer_id': 2089354, 'author': 'Steven A. Lowe', 'author_id': 9345, 'author_profile': 'https://Stackoverflow.com/users/9345', 'pm_score': 0, 'selected': False, 'text': '<p>Polymorphism <em>does not require inheritance</em>. Polymorphism is what you get when multiple objects implement the same message signature (method).</p>\n'}]
['https://Stackoverflow.com/questions/11854', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1256/']