Skip to content
dreamcode
dreamcode
Map
Inheritance
Lesson 10 of 21
+15 XP on finish
C# OOPChapter 3 · C# Object Oriented Programming

Inheritance and polymorphism

Inheritance allows a class to derive from a base class, inheriting fields, properties, and methods. Use virtual in the base class and override in the subclass to redefine behavior.

Worked example
class SkyObject {
    public virtual void Describe() {
        Console.WriteLine("Object in sky");
    }
}

class Star : SkyObject {
    public override void Describe() {
        Console.WriteLine("Bright star");
    }
}

How it reads

  • class Star : SkyObject indicates that Star inherits from SkyObject
  • virtual void Describe() allows subclasses to redefine this method
  • override void Describe() redefines the method in the subclass
Cloud tip: C# only supports single inheritance for classes. A subclass can only inherit from one direct base class.

Check your understanding

0 / 2

Answer all 2 to complete this lesson and earn 15 XP.

  1. 1. Which symbol denotes inheritance in C#?
  2. 2. Which keyword marks a base class method, one that already has a body, as safe to override?
Answer every question to unlock the next lesson.