Inheritance: Reusing Code
Reuse and extend existing code using inheritance in C#.
Inheritance: Reusing Code is a free C# Academy lesson on CoddyKit. This is lesson 6 of 7. 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 7 lessons in total.
Intro to Inheritance
Inheritance: Reusing Code
Welcome to the next lesson! In this lesson, you’ll learn about inheritance, one of the core principles of Object-Oriented Programming, and how it enables you to reuse code and build hierarchies. Let’s get started!

What is Inheritance?
Inheritance allows one class (the child or derived class) to acquire the attributes and methods of another class (the parent or base class). It enables code reuse and hierarchical class relationships.
Example: A "Car" class could serve as a base class for "ElectricCar" and "SportsCar" classes, sharing common properties like "Color" and "Speed."
Defining a Base Class
A base class contains common properties and methods that can be inherited by derived classes. Example:
Key Point: Define reusable code in the base class.
using System;
class Vehicle {
public string Color;
public void Drive() {
Console.WriteLine("The vehicle is driving.");
}
}
Creating a Derived Class
A derived class inherits the properties and methods of a base class. Use the : symbol to indicate inheritance. Example:
Key Point: Derived classes can add new features or override base class functionality.
using System;
class Vehicle {
public string Color;
public void Drive() {
Console.WriteLine("The vehicle is driving.");
}
}
class Car : Vehicle {
public int NumberOfDoors;
public void ShowDetails() {
Console.WriteLine("Color: " + Color);
Console.WriteLine("Doors: " + NumberOfDoors);
}
}
class Program {
static void Main() {
Car myCar = new Car();
myCar.Color = "Red"; // Inherited from Vehicle
myCar.NumberOfDoors = 4;
myCar.Drive(); // Inherited from Vehicle
myCar.ShowDetails();
}
}
Overriding Methods
Derived classes can override methods in the base class to provide specialized behavior. Use the virtual keyword in the base class and the override keyword in the derived class. Example:
Tip: Overriding is useful for customizing behavior in derived classes.
using System;
class Vehicle {
public virtual void Drive() {
Console.WriteLine("The vehicle is driving.");
}
}
class SportsCar : Vehicle {
public override void Drive() {
Console.WriteLine("The sports car is driving fast!");
}
}
class Program {
static void Main() {
SportsCar myCar = new SportsCar();
myCar.Drive(); // Calls the overridden method
}
}
Base Keyword
The base keyword is used to access members of the base class from within a derived class. Example:
Tip: Use base to extend or supplement base class behavior.
using System;
class Vehicle {
public string Color;
public virtual void Drive() {
Console.WriteLine("The vehicle is driving.");
}
}
class Truck : Vehicle {
public override void Drive() {
base.Drive(); // Calls the base class method
Console.WriteLine("The truck is carrying a heavy load.");
}
}
class Program {
static void Main() {
Truck myTruck = new Truck();
myTruck.Color = "Blue";
myTruck.Drive();
}
}
Practice Challenge
Create a base class called Animal with a method Speak() that prints "The animal makes a sound." Create derived classes Dog and Cat that override Speak() to print "The dog barks." and "The cat meows." Example:
using System;
class Animal {
public virtual void Speak() {
Console.WriteLine("The animal makes a sound.");
}
}
class Dog : Animal {
public override void Speak() {
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal {
public override void Speak() {
Console.WriteLine("The cat meows.");
}
}
class Program {
static void Main() {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.Speak();
myCat.Speak();
}
}
Best Practices for Inheritance
Here are some best practices:
- Use inheritance only when there is a clear "is-a" relationship (e.g., a Car is a Vehicle).
- Keep base classes as general as possible.
- Use
sealedto prevent further inheritance when necessary.
Tip: Inheritance should simplify, not complicate, your codebase.
Great Job!
Congratulations! You’ve learned how to use inheritance to reuse code and build class hierarchies in C#. In the next lesson, we’ll dive into polymorphism, another powerful concept of OOP. Let’s keep coding!

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 “Inheritance: Reusing Code” lesson free?
Yes — the full text of “Inheritance: Reusing Code” 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 7 lessons in total.
What will I learn in “Inheritance: Reusing Code”?
Reuse and extend existing code using inheritance in C#. 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 6 of 7.
How long does the “Inheritance: Reusing Code” 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
- What is Object-Oriented Programming?
- Defining Classes and Objects
- Understanding Properties and Methods
- Constructors in C#
- Encapsulation and Access Modifiers
- Inheritance: Reusing Code
- Polymorphism and Method Overriding