Background: 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:
Suppose I have a class called LivingOrganism
. Every living creature can use this class as base and inherit the functionality that is common to all living organisms.
When I was working on some of these derived
classes, I found out that bananas and humans are very similar. Although it doesn't make much sense and they look nothing alike, they apparently have most of their "functionality" in common.
Duplication in code is bad, so I wrote a new class to reduce maintenance costs. This class is called: BananaHuman
. My Human
class and Banana
class inherit from BananaHuman
.
Problem:
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 LivingCreatures.dll. And they won't understand why intellisense suggests BananaHuman when they type 'B'.
And consider the following piece of code:
//Valid and makes sense.
foreach(LivingOrganism foo in cityOfNeyYork) { /*embedded statement*/ }
But imagine how weird/confusing it would look if we substitute Living Organism
with BananaHuman
.
I can't make BananaHuman
private
, protected
or protected internal
(Elements defined in a namespace cannot be explicitly declared that way). I also can't make it internal
, because Human
and Banana
have to be public
. If I try this, I get an error message saying there is an inconsistent accessibility
issue.
I feel like I am missing the obvious, but what can/should I do? I am left with a couple of options/questions:
- Is it possible to "hide"
BananaHuman
to avoid confusion? - Should I rewrite
BananaHuman
to something very long and technical such asDnaRelatedOrganismsType[X]
, where "X" describes their unique relation? - Should I just delete
BananaHuman
, letHuman
andBanana
inherit fromLivingOrganism
and do the extra maintenance when something needs changing? - Is there another solution I am completely missing?
I searched around and couldn't quite find a "fixed pattern" for this situation. I found this 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.
Any help is greatly appreciated!