I have no clue why the constuctor of the Car class does not compile.
"Cannot implicitly convert type 'System.Collections.Generic.List<ConsoleApp1.Tire>' to 'System.Collections.Generic.List<ConsoleApp1.IWheel>'"
using System.Collections.Generic;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
IVehicle myCar = new Car();
myCar.AddWheel();
var countWheels = CountWheels(myCar);
}
static int CountWheels(IVehicle vehicle)
{
return vehicle.Wheels.Count;
}
}
// Interfaces
public interface IVehicle
{
public List<IWheel> Wheels { get; set; }
public void AddWheel();
}
public interface IWheel
{
public int Radius { get; set; }
}
// Abstract base classes
public abstract class Vehicle : IVehicle
{
public List<IWheel> Wheels { get; set; }
public abstract void AddWheel();
}
public abstract class Wheel : IWheel
{
public int Radius { get; set; }
}
// Implementations
public class Car : Vehicle
{
public override void AddWheel()
{
var newTire = new Tire();
this.Wheels.Add(newTire);
}
public Car()
{
this.Wheels = new List<Tire>();
}
}
public class Tire : Wheel
{
public int MinimumPressure { get; set; }
}
}
Every Wheel implements IWheel and every Tire is a Wheel. Thereby every Tire implements IWheel implicitly, right?
So, why cant I assign a List of Tires to a List of IWheels?
Thank you for your help guys. I obviously completely misunderstood something about polymorphism.