Bird
Raised Fist0

Given these classes:

hard🚀 Application Q15 of Q15
C Sharp (C#) - Polymorphism and Abstract Classes
Given these classes:
class Vehicle {
  public virtual string Describe() => "Vehicle";
}
class Car : Vehicle {
  public override string Describe() => "Car";
}
class SportsCar : Car {
  public override string Describe() => "SportsCar";
}

Vehicle v = new SportsCar();
Car c = new SportsCar();
SportsCar s = new SportsCar();

Console.WriteLine(v.Describe());
Console.WriteLine(c.Describe());
Console.WriteLine(s.Describe());
What is the output?
AVehicle\nCar\nSportsCar
BCar\nCar\nCar
CVehicle\nVehicle\nVehicle
DSportsCar\nSportsCar\nSportsCar
Step-by-Step Solution
Solution:
  1. Step 1: Identify actual object type for all references

    All variables v, c, and s reference a SportsCar object.
  2. Step 2: Apply runtime polymorphism for Describe()

    Since Describe is overridden in SportsCar, all calls print "SportsCar" regardless of reference type.
  3. Final Answer:

    SportsCar\nSportsCar\nSportsCar -> Option D
  4. Quick Check:

    All calls use SportsCar override [OK]
Quick Trick: Actual object type decides method, not reference type [OK]
Common Mistakes:
MISTAKES
  • Assuming base class method runs for base type variable
  • Confusing reference type with object type
  • Ignoring override in most derived class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Sharp (C#) Quizzes