C# Academy · Lesson

virtual/override/sealed; abstract classes

Learn virtual methods, overriding, sealing overrides, and abstract classes; see how base references dispatch to derived implementations.

Lesson 1 of 38 steps

virtual/override/sealed; abstract classes is a free C# Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.

Inheritance basics

Goal: Reuse and customize behavior through inheritance.

  • virtual in base
  • override in derived
  • sealed override to stop further changes
  • abstract class defines a contract + shared code

virtual + override

virtual enables overriding. Calls through a base reference use the derived override (dynamic dispatch).

using System;

// Base method is virtual so derived classes can customize it
public class Shape
{
  public virtual double Area()
  {
    return 0.0; // default
  }
}

public class Rectangle : Shape
{
  public int Width { get; private set; }
  public int Height { get; private set; }

  public Rectangle(int w, int h)
  {
    Width = w; Height = h;
  }

  public override double Area()
  {
    return Width * Height;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Shape s = new Shape();
    Shape r = new Rectangle(3, 4); // base reference
    Console.WriteLine("Shape area = " + s.Area());
    Console.WriteLine("Rectangle area = " + r.Area()); // calls Rectangle.Area()
  }
}

sealed override

Use sealed override to lock a method at a certain level in the hierarchy.

using System;

public class BaseLogger
{
  public virtual string Prefix()
  {
    return "[LOG]";
  }
}

public class ConsoleLogger : BaseLogger
{
  public sealed override string Prefix()
  {
    return "[CONSOLE]";
  }
}

// Further derived class cannot override Prefix again:
// public class FancyLogger : ConsoleLogger
// {
//   public override string Prefix() { return "[FANCY]"; } // compile error
// }

public class Program
{
  public static void Main(string[] args)
  {
    ConsoleLogger log = new ConsoleLogger();
    Console.WriteLine(log.Prefix() + " Ready");
  }
}

Abstract classes

Abstract classes define required members (abstract) and can also provide shared code.

using System;

// Abstract class: cannot be instantiated; must implement abstract members
public abstract class Animal
{
  public abstract string Speak(); // no body here
  public string Info()
  {
    return "Animal says: " + Speak(); // shared code uses the abstract member
  }
}

public class Dog : Animal
{
  public override string Speak()
  {
    return "Woof";
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    // Animal a = new Animal(); // compile error: abstract
    Animal d = new Dog();
    Console.WriteLine(d.Info()); // calls Dog.Speak()
  }
}

Virtual vs abstract

Choose:

  • virtual: provide a default body that derived types may override.
  • abstract: no body; derived types must implement.
  • sealed override: stop further overrides once behavior is fixed.

Polymorphic calls

Polymorphism: a method accepts the base type and calls overridden members on derived instances.

using System;

public class Notifier
{
  public virtual string Channel() { return "Base"; }
}

public class EmailNotifier : Notifier
{
  public override string Channel() { return "Email"; }
}

public class SmsNotifier : Notifier
{
  public override string Channel() { return "SMS"; }
}

public class Program
{
  // Works with any Notifier (polymorphism)
  static void Send(Notifier n, string msg)
  {
    Console.WriteLine("[" + n.Channel() + "] " + msg);
  }

  public static void Main(string[] args)
  {
    Send(new EmailNotifier(), "Hello");
    Send(new SmsNotifier(), "Hi");
  }
}

virtual/override/sealed rule

Quick check: Which statement is correct about virtual, override, and sealed in C#?

Recap

Recap: Mark base methods virtual, override in derived types, and use sealed override to lock behavior. Use abstract classes to require implementations while sharing common code.

Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “virtual/override/sealed; abstract classes” lesson free?

Yes — the full text of “virtual/override/sealed; abstract classes” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 3 lessons in total.

What will I learn in “virtual/override/sealed; abstract classes”?

Learn virtual methods, overriding, sealing overrides, and abstract classes; see how base references dispatch to derived implementations. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “virtual/override/sealed; abstract classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. virtual/override/sealed; abstract classes
  2. Interfaces (intro) & multiple interface implementation
  3. Polymorphism & dynamic dispatch
← Back to C# Academy