Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Runtime polymorphism execution in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a base class method that can be overridden.

C Sharp (C#)
public class Animal {
    public virtual void Speak() {
        Console.[1]("Animal speaks");
    }
}
Drag options to blanks, or click blank then click option'
ADisplay
BPrint
COutput
DWriteLine
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist in Console class like Print or Display.
2fill in blank
medium

Complete the code to override the Speak method in the derived class.

C Sharp (C#)
public class Dog : Animal {
    public override void [1]() {
        Console.WriteLine("Dog barks");
    }
}
Drag options to blanks, or click blank then click option'
ASpeak
BTalk
CSound
DMakeNoise
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name that does not override the base method.
3fill in blank
hard

Fix the error in calling the Speak method using a base class reference to a derived class object.

C Sharp (C#)
Animal myAnimal = new Dog();
myAnimal.[1]();
Drag options to blanks, or click blank then click option'
ABark
BTalk
CSpeak
DMakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that only exists in the derived class or using wrong method names.
4fill in blank
hard

Fill both blanks to create a base class reference to a derived class object and call the overridden method.

C Sharp (C#)
Animal [1] = new [2]();
[1].Speak();
Drag options to blanks, or click blank then click option'
AmyAnimal
BDog
CAnimal
DanimalObj
Attempts:
3 left
💡 Hint
Common Mistakes
Using the base class name for the object creation or mismatching variable names.
5fill in blank
hard

Fill all three blanks to implement runtime polymorphism with two derived classes overriding the base method.

C Sharp (C#)
public class Cat : Animal {
    public override void [1]() {
        Console.WriteLine("Cat meows");
    }
}

Animal [2] = new Dog();
Animal [3] = new Cat();
[2].Speak();
[3].Speak();
Drag options to blanks, or click blank then click option'
ASpeak
BdogAnimal
CcatAnimal
DMakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names or inconsistent variable names.

Practice

(1/5)
1. What does runtime polymorphism in C# allow you to do?
easy
A. Create multiple instances of the same class
B. Change variable types at runtime
C. Call derived class methods through a base class reference
D. Use static methods without creating objects

Solution

  1. Step 1: Understand runtime polymorphism concept

    Runtime polymorphism allows a base class reference to call methods overridden in derived classes.
  2. Step 2: Identify correct behavior

    This means the actual method called depends on the object's real type, not the reference type.
  3. Final Answer:

    Call derived class methods through a base class reference -> Option C
  4. Quick Check:

    Runtime polymorphism = base ref calls derived method [OK]
Hint: Think: base class ref calls derived method at runtime [OK]
Common Mistakes:
  • Confusing polymorphism with changing variable types
  • Thinking static methods are polymorphic
  • Believing polymorphism creates multiple instances
2. Which keyword is used in C# to allow a method to be overridden in a derived class?
easy
A. virtual
B. override
C. new
D. abstract

Solution

  1. Step 1: Identify keyword to enable overriding

    The base class method must be marked with virtual to allow overriding.
  2. Step 2: Understand roles of keywords

    override is used in derived classes, virtual in base classes.
  3. Final Answer:

    virtual -> Option A
  4. Quick Check:

    Base method uses virtual to allow override [OK]
Hint: Base method uses virtual; derived uses override [OK]
Common Mistakes:
  • Using override in base class instead of virtual
  • Confusing new keyword with override
  • Thinking abstract is required for all overrides
3. What is the output of this C# code?
class Animal {
  public virtual string Speak() => "Animal sound";
}
class Dog : Animal {
  public override string Speak() => "Bark";
}
class Cat : Animal {
  public override string Speak() => "Meow";
}

Animal a = new Dog();
Console.WriteLine(a.Speak());
medium
A. Animal sound
B. Bark
C. Meow
D. Compile error

Solution

  1. Step 1: Identify object type and method called

    Variable a is of type Animal but references a Dog object.
  2. Step 2: Apply runtime polymorphism

    Since Speak is virtual and overridden in Dog, the Dog version runs, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Base ref calls Dog's Speak() = Bark [OK]
Hint: Base ref calls derived override method at runtime [OK]
Common Mistakes:
  • Expecting base class method output
  • Confusing object type with reference type
  • Thinking compile error due to override
4. Identify the error in this C# code related to runtime polymorphism:
class Base {
  public override void Show() {
    Console.WriteLine("Base Show");
  }
}
class Derived : Base {
  public override void Show() {
    Console.WriteLine("Derived Show");
  }
}
medium
A. Base class method must be virtual, not override
B. Derived class method cannot override base method
C. Missing semicolon after method declaration
D. No error, code is correct

Solution

  1. Step 1: Check base class method declaration

    Base class method incorrectly uses override instead of virtual.
  2. Step 2: Understand override rules

    Only derived classes use override; base class must use virtual to allow overriding.
  3. Final Answer:

    Base class method must be virtual, not override -> Option A
  4. Quick Check:

    Base method needs virtual keyword [OK]
Hint: Base method uses virtual, not override [OK]
Common Mistakes:
  • Using override in base class method
  • Thinking override is allowed without virtual
  • Ignoring method signature correctness
5. 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?
hard
A. Vehicle\nCar\nSportsCar
B. Car\nCar\nCar
C. Vehicle\nVehicle\nVehicle
D. SportsCar\nSportsCar\nSportsCar

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]
Hint: Actual object type decides method, not reference type [OK]
Common Mistakes:
  • Assuming base class method runs for base type variable
  • Confusing reference type with object type
  • Ignoring override in most derived class