I have an abstract class that takes a generic type parameter, so that the user can define correct functionality in child classes:
public abstract class Fruit<TFruit>{
public abstract TFruit CutFruit();
public abstract float TypeIndependentMethod();
}
An outside class wants to call a type-independent abstract method in the class, and it does not need to know the type parameter of the child class. It only wants to call that class's implementation of TypeIndependentMethod()
:
public class FruitLabeler
{
public Fruit fruit = null;
public void WriteLabel(){
float f = fruit.TypeIndependentMethod();
}
}
The issue is that I cannot declare public Fruit fruit
without a type parameter, the compiler demands that I declare Fruit<Orange> fruit
or Fruit<Apple> fruit
, etc. But I don't want my FruitLabeler
to only work for Oranges or Apples, I want it to work for any fruit.
How can I declare an instance of Fruit
that is type-independent, or how can I call it's generic-type-independent methods?