The following combines some of the best aspects of several other answers as well as a technique to allow the key aspect of Cat
having an Excrement
property of the required RadioactivePoo
type, but being able to return that as merely Poo
if we only know we've got an AnimalBase
rather than specifically a Cat
.
Callers aren't required to use generics, even though they are present in the implementations, nor to call a differently-named function to get special Poo
.
The intermediate class AnimalWithSpecialisations
serves only to seal the Excrement
property, connecting it via a non-public SpecialPoo
property to the derived class AnimalWithSpecialPoo<TPoo>
which has an Excrement
property of a derived return type.
If Cat
is the only animal whose Poo
is special in any way, or we don't want the type of Excrement
to be the main defining feature of a Cat
, the intermediate generic class could be skipped in the hierarchy, so that Cat
derives directly from AnimalWithSpecialisations
, but if there are several different animals whose primary characteristic is that their Poo
is special in some way, separating out the "boilerplate" into intermediate classes helps to keep the Cat
class itself fairly clean, albeit at the cost of a couple of extra virtual function calls.
The example code shows that most of the expected operations work "as expected".
public interface IExcretePoo<out TPoo>
where TPoo : Poo
{
TPoo Excrement { get; }
}
public class Poo
{ }
public class RadioactivePoo : Poo
{ }
public class AnimalBase : IExcretePoo<Poo>
{
public virtual Poo Excrement { get { return new Poo(); } }
}
public class Dog : AnimalBase
{
// No override, just return normal poo like normal animal
}
public abstract class AnimalWithSpecialisations : AnimalBase
{
// this class connects AnimalBase to AnimalWithSpecialPoo<TPoo>
public sealed override Poo Excrement { get { return SpecialPoo; } }
// if not overridden, our "special" poo turns out just to be normal animal poo...
protected virtual Poo SpecialPoo { get { return base.Excrement; } }
}
public abstract class AnimalWithSpecialPoo<TPoo> : AnimalWithSpecialisations, IExcretePoo<TPoo>
where TPoo : Poo
{
sealed protected override Poo SpecialPoo { get { return Excrement; } }
public new abstract TPoo Excrement { get; }
}
public class Cat : AnimalWithSpecialPoo<RadioactivePoo>
{
public override RadioactivePoo Excrement { get { return new RadioactivePoo(); } }
}
class Program
{
static void Main(string[] args)
{
Dog dog = new Dog();
Poo dogPoo = dog.Excrement;
Cat cat = new Cat();
RadioactivePoo catPoo = cat.Excrement;
AnimalBase animal = cat;
Poo animalPoo = catPoo;
animalPoo = animal.Excrement;
AnimalWithSpecialPoo<RadioactivePoo> radioactivePooingAnimal = cat;
RadioactivePoo radioactivePoo = radioactivePooingAnimal.Excrement;
IExcretePoo<Poo> pooExcreter = cat; // through this interface we don't know the Poo was radioactive.
IExcretePoo<RadioactivePoo> radioactivePooExcreter = cat; // through this interface we do.
// we can replace these with the dog equivalents:
animal = dog;
animalPoo = dogPoo;
pooExcreter = dog;
// but we can't do:
// radioactivePooExcreter = dog;
// radioactivePooingAnimal = dog;
// radioactivePoo = dogPoo;
}