Inheritance: Reusing Code
Reuse and extend existing code using inheritance in C#.
Inheritance: Reusing Code is a free C# Academy lesson on CoddyKit — 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, one of 7 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
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!

2
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."
3
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.");
}
}
4
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();
}
}
5
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
}
}
6
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();
}
}
7
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();
}
}
8
9
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.
10
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!

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, and the C# Academy course includes 7 lessons in total. 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.
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; this is — lesson 6 of 7, so you can start here or from the beginning and move at your own pace.
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.