Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

polymorphism-csharp

Polymorphism

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Polymorphism: the syntax, patterns, and safety habits you need before advancing in C#.

Virtual dispatch and vtables are classic interview topics—misuse causes performance and slicing bugs.

You will apply Polymorphism in contexts like: .NET services, Unity games, and Windows-centric tooling.

Write C# with Console.WriteLine (top-level or Program), click Run on server—the dev runner uses dotnet build/run on a temp net8 project (requires .NET SDK; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

virtual/override enables runtime polymorphism through base class references—calls dispatch to the actual derived type. Without virtual, methods are hidden, not overridden—unlike polymorphic dispatch in Java where instance methods are virtual by default.

Override example

class Animal {
    public virtual void Speak() => Console.WriteLine("...");
}
class Dog : Animal {
    public override void Speak() => Console.WriteLine("woof");
}

Animal pet = new Dog();
pet.Speak();  // woof

Important interview questions and answers

  1. Q: virtual vs override?
    A: virtual allows derived override; override replaces base implementation for polymorphic calls.
  2. Q: new keyword hiding?
    A: new hides a base member without polymorphism—calls through base type still use base implementation.

Self-check

  1. What keyword enables polymorphic dispatch?
  2. What happens if base method is not virtual?

Tip: Mark base methods virtual and derived methods override—without virtual, calls resolve at compile time to the base type.

Interview prep

virtual vs override?

virtual on the base enables overriding; override on derived replaces behavior—calls through base references dispatch at runtime.

new keyword hiding?

new hides a base member without overriding—polymorphism through base references still calls the base version.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • override vs new?
  • virtual default?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump