0Pricing
C# Academy · Lesson

Abstract Classes

Explore abstract classes and their role in creating a shared structure for derived classes.

Abstract Classes is a free C# Academy lesson on CoddyKit — lesson 2 of 6. 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 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Abstract Classes

Welcome to the next lesson! In this lesson, you’ll learn about abstract classes in C#, their purpose, and how they differ from interfaces. Let’s get started!

Abstract Classes — illustration 1

2

What is an Abstract Class?

An abstract class is a class that cannot be instantiated directly. It serves as a blueprint for other classes and may include abstract methods, which must be implemented by derived classes.

Key Point: Abstract classes can include both fully implemented methods and methods that are only declared (abstract methods).

3

Defining an Abstract Class

To define an abstract class, use the abstract keyword. Example:

Tip: Use abstract classes to provide a common base for related classes.

using System;

abstract class Animal {
    public abstract void Speak(); // Abstract method
    public void Sleep() { // Non-abstract method
        Console.WriteLine("The animal is sleeping.");
    }
}

4

Implementing an Abstract Class

Derived classes must implement all abstract methods of the abstract class. Example:

Key Point: Abstract methods define behavior that must be implemented by derived classes.

using System;

abstract class Animal {
    public abstract void Speak(); // Abstract method
    public void Sleep() {
        Console.WriteLine("The animal is sleeping.");
    }
}

class Dog : Animal {
    public override void Speak() {
        Console.WriteLine("The dog barks.");
    }
}

class Program {
    static void Main() {
        Dog myDog = new Dog();
        myDog.Speak(); // Outputs: The dog barks.
        myDog.Sleep(); // Outputs: The animal is sleeping.
    }
}

5

Abstract Classes vs Interfaces

Both abstract classes and interfaces are used for abstraction, but they differ in several ways:

  • Abstract classes can have both implemented and abstract methods, while interfaces can only have abstract methods (prior to C# 8.0).
  • Abstract classes can include fields, constructors, and access modifiers for members.
  • A class can inherit only one abstract class but can implement multiple interfaces.

Tip: Use abstract classes for closely related classes and interfaces for broader abstraction.

6

When to Use Abstract Classes

Abstract classes are best used when:

  • You want to define a common base class with shared functionality.
  • You need to include non-abstract methods that provide a default implementation.
  • You are working with related classes that share a common hierarchy.

Example: A "Shape" abstract class for "Circle" and "Rectangle" classes.

7

Practice Challenge

Create an abstract class Shape with an abstract method CalculateArea(). Implement this method in derived classes Rectangle and Circle. Example:

using System;

abstract class Shape {
    public abstract double CalculateArea();
}

class Rectangle : Shape {
    public double Width { get; set; }
    public double Height { get; set; }

    public override double CalculateArea() {
        return Width * Height;
    }
}

class Circle : Shape {
    public double Radius { get; set; }

    public override double CalculateArea() {
        return Math.PI * Radius * Radius;
    }
}

class Program {
    static void Main() {
        Shape rectangle = new Rectangle { Width = 5, Height = 4 };
        Shape circle = new Circle { Radius = 3 };

        Console.WriteLine("Rectangle Area: " + rectangle.CalculateArea());
        Console.WriteLine("Circle Area: " + circle.CalculateArea());
    }
}

8

9

Best Practices for Abstract Classes

Here are some best practices:

  • Use abstract classes when you have shared functionality that needs to be inherited.
  • Keep the abstract class focused and avoid adding unnecessary members.
  • Combine abstract classes with interfaces when appropriate.

Tip: Abstract classes should provide a solid foundation for derived classes.

10

Great Job!

Congratulations! You’ve learned how to use abstract classes to define shared behavior and enforce consistency across derived classes. In the next lesson, we’ll explore static members and methods in C#. Let’s keep coding!

Abstract Classes — illustration 10

Frequently asked questions

Is the “Abstract Classes” lesson free?

Yes — the full text of “Abstract Classes” is free to read here on the web, and the C# Academy course includes 6 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 “Abstract Classes”?

Explore abstract classes and their role in creating a shared structure for derived classes. 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 2 of 6, so you can start here or from the beginning and move at your own pace.

How long does the “Abstract Classes” 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

  1. Introduction to Interfaces
  2. Abstract Classes
  3. Static Members and Methods
  4. Exploring Enums and Structs
  5. Delegates and Events
  6. Working with Collections: Lists, Dictionaries, and Queues
← Back to C# Academy