Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking abstract classes invisible; or: hiding my BananaHuman
    primarykey
    data
    text
    <p><strong>Background:</strong> I am still a C# novice and this is my first big project with inheritance. The following story is a simplified example of my current situation:</p> <p>Suppose I have a class called <code>LivingOrganism</code>. Every living creature can use this class as base and inherit the functionality that is common to all living organisms.</p> <p>When I was working on some of these <code>derived</code> classes, I found out that bananas and humans <strong><a href="http://www.nhm.ac.uk/nature-online/evolution/what-is-the-evidence/morphology/dna-molecules/" rel="nofollow noreferrer">are very similar</a></strong>. Although it doesn't make much sense and they look nothing alike, they apparently have most of their "functionality" in common.</p> <p>Duplication in code is bad, so I wrote a new class to reduce maintenance costs. This class is called: <code>BananaHuman</code>. My <code>Human</code> class and <code>Banana</code> class inherit from <code>BananaHuman</code>.</p> <hr> <p><strong>Problem:</strong></p> <p>I have no problem with my BananaHuman (i.e. I understand what it means and why it exists). But eventually, other people (even those who don't (fully) understand inheritance) will have to use my <em>LivingCreatures.dll</em>. And they won't understand why <em>intellisense</em> suggests BananaHuman when they type 'B'. </p> <p>And consider the following piece of code:</p> <pre><code>//Valid and makes sense. foreach(LivingOrganism foo in cityOfNeyYork) { /*embedded statement*/ } </code></pre> <p>But imagine how weird/confusing it would look if we substitute <code>Living Organism</code> with <code>BananaHuman</code>.</p> <p>I can't make <code>BananaHuman</code> <code>private</code>, <code>protected</code> or <code>protected internal</code> (Elements defined in a namespace cannot be explicitly declared that way). I also can't make it <code>internal</code>, because <code>Human</code> and <code>Banana</code> <strong>have</strong> to be <code>public</code>. If I try this, I get an error message saying there is an <code>inconsistent accessibility</code> issue.</p> <p>I feel like I am missing the obvious, but what can/should I do? I am left with a couple of options/questions:</p> <ol> <li>Is it possible to "hide" <code>BananaHuman</code> to avoid confusion? </li> <li>Should I rewrite <code>BananaHuman</code> to something very long and technical such as <code>DnaRelatedOrganismsType[X]</code>, where "X" describes their unique relation? </li> <li>Should I just delete <code>BananaHuman</code>, let <code>Human</code> and <code>Banana</code> inherit from <code>LivingOrganism</code> and do the extra maintenance when something needs changing? </li> <li>Is there another solution I am completely missing?</li> </ol> <p>I searched around and couldn't quite find a <strong>"fixed pattern"</strong> for this situation. I found <a href="https://stackoverflow.com/questions/10698020/how-to-hide-the-entire-class">this</a> question with a similar title, but I don't know if the answers are applicable because it appears that he is asking something completely different. </p> <p>Any help is greatly appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload