Reputation:
I have a baseclass called RottenFruit, a class called RottenApple that inherints from it.
Now i want the RottenFruit class to inherit a class called Fruit, which is a base class for the Apple class, so I can add a Fruit object under each RottenFruit object, making it generic in terms of the types of fruit.
abstract class RottenFruits : Fruit
{
public virtual Fruit fruit { get; set; };
bool isRotten { get; set; }
}
class RottenApple : RottenFruits
{
public override Apple fruit { get; set; };
}
If i Try this I get an error in the derived class saying
Type must be Fruit to match overridden member RottenFruits.fruit Why dosen't the type match when Fruit is a baseclass of Apple?
Edit: Lets say I have multiple RottenFruits: RottenBanana, RottenPear, etc. and I want to gather them all in an array. RottenFruits will expect a type parameter of Fruit if I use a generic type
RottenFruits[] fruits = new {
new RottenApple(){fruit = new Apple()},
new RottenBanana(){fruit = new Banana()
}
Upvotes: 0
Views: 250
Reputation: 62488
You cannot do the way you are trying it. i think what you can do is use generics here:
class Fruit
{
}
class abstract RottenFruits<T> : Fruit where T : class
{
public T fruit;
bool isRotten { get; set; }
}
and now you can do it:
class RottenApple : RottenFruits<Apple>
{
}
Upvotes: 4