Back to questions
JuniorTheoryCommon
What does the program print?
Vehicle a = new Car();
Vehicle b = new Bike();
Console.WriteLine(a.Who() + b.Who());
class Vehicle { public virtual string Who() => "V"; }
class Car : Vehicle { public override string Who() => "C"; }
class Bike : Vehicle { public new string Who() => "B"; }CV
- Expecting
CB:newhides the method instead of overriding it, so through aVehiclevariable the version ofVehicleruns. - Expecting
VV: withoverridethe version comes from the object's type, soa.Who()runsCar.Whoeven through aVehiclevariable.
- What does
new Bike().Who()return, and why? - Which compiler warning appears if
newis removed fromBike.Who?