</>
Skip to content
C# lessons (22/34)

C# — Inheritance

Basic inheritance

using System;

public class Animal
{
    public string Name { get; set; }

    public void Eat()
    {
        Console.WriteLine($"{Name} is eating");
    }

    public virtual void Speak()
    {
        Console.WriteLine($"{Name} makes a sound");
    }
}

public class Dog : Animal
{
    public void Fetch()
    {
        Console.WriteLine($"{Name} is fetching");
    }

    public override void Speak()
    {
        Console.WriteLine($"{Name} says Woof!");
    }
}

class Program
{
    static void Main()
    {
        var dog = new Dog { Name = "Rex" };
        dog.Eat();     // Inherited
        dog.Speak();   // Overridden
        dog.Fetch();   // Dog-specific
    }
}

Access modifiers in inheritance

public class Base
{
    public int Pub = 1;
    protected int Prot = 2;
    private int Priv = 3;
}

public class Derived : Base
{
    public void Show()
    {
        Console.WriteLine(Pub);   // OK
        Console.WriteLine(Prot);  // OK
        // Console.WriteLine(Priv); // Error
    }
}

Constructor chaining

using System;

public class Person
{
    public string Name { get; }

    public Person(string name)
    {
        Name = name;
        Console.WriteLine("Person constructed");
    }
}

public class Employee : Person
{
    public string Department { get; }

    public Employee(string name, string dept) : base(name)
    {
        Department = dept;
        Console.WriteLine("Employee constructed");
    }
}

class Program
{
    static void Main()
    {
        var e = new Employee("Alice", "Engineering");
        // Output: Person constructed → Employee constructed
    }
}

Abstract classes

using System;

public abstract class Shape
{
    public abstract double Area();
    public abstract string Type { get; }

    public void Describe()
    {
        Console.WriteLine($"{Type}: Area = {Area():F2}");
    }
}

public class Circle : Shape
{
    public double Radius { get; }
    public override string Type => "Circle";

    public Circle(double radius) => Radius = radius;
    public override double Area() => Math.PI * Radius * Radius;
}

public class Rectangle : Shape
{
    public double Width { get; }
    public double Height { get; }
    public override string Type => "Rectangle";

    public Rectangle(double w, double h) { Width = w; Height = h; }
    public override double Area() => Width * Height;
}

class Program
{
    static void Main()
    {
        Shape[] shapes = { new Circle(5), new Rectangle(4, 6) };
        foreach (var s in shapes)
            s.Describe();
    }
}

Sealed classes

using System;

public class Base
{
    public virtual void Method() => Console.WriteLine("Base");
}

public sealed class Final : Base
{
    public override void Method() => Console.WriteLine("Final");
}

// public class More : Final { } // Error: cannot derive from sealed

class Program
{
    static void Main()
    {
        var f = new Final();
        f.Method(); // Final
    }
}

Polymorphism

using System;

public class Animal
{
    public virtual string Speak() => "...";
}

public class Dog : Animal
{
    public override string Speak() => "Woof!";
}

public class Cat : Animal
{
    public override string Speak() => "Meow!";
}

class Program
{
    static void Main()
    {
        Animal[] animals = { new Dog(), new Cat(), new Animal() };
        foreach (var a in animals)
        {
            Console.WriteLine(a.Speak()); // Polymorphic call
        }
    }
}

is and as operators

using System;

class Program
{
    static void Main()
    {
        object obj = "Hello";

        // is: type check
        if (obj is string s)
        {
            Console.WriteLine($"String of length {s.Length}");
        }

        // as: safe cast (returns null if fails)
        string str = obj as string;
        Console.WriteLine(str?.Length); // 5

        int? num = obj as int?;
        Console.WriteLine(num); // null

        // Pattern matching
        Console.WriteLine(obj is string { Length: > 3 }); // True
    }
}

Mini Practice

Write C# code that:

  1. Creates an abstract Vehicle class with Start() and Stop()
  2. Implements Car and Motorcycle derived classes
  3. Uses polymorphism with an array of Vehicle references
  4. Demonstrates is and as operators

Up Next

In the next lesson, you'll learn about Interfaces — defining contracts in C#.

Related Topics

Frequently Asked Questions about Inheritance

What is Inheritance in C#?

Inheritance is a fundamental concept in C#. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Inheritance?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Inheritance.

Why is Inheritance important in C#?

Inheritance is essential for C# development. Understanding this concept will help you write better code and solve real-world problems more effectively.