← Back to C# Course | Chapter 7: Inheritance | Lesson 1 of 6

Inheritance Basics

In this page:

  1. Inheritance Basics

Inheritance Basics

Inheritance lets a class (the derived class) reuse and extend the fields and methods of another class (the base class) using the : syntax. The derived class automatically gets everything public and protected from its base. This models "is-a" relationships, like a Dog being an Animal.

Note: C# only supports single inheritance for classes — a class can inherit from just one base class.

Example: Inheritance Basics

markup
using System;

class Animal
{
    public string Name;

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

class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine($"{Name} says Woof!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Dog d = new Dog { Name = "Rex" };
        d.Eat();
        d.Bark();
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.