Skip to main content
C# Fundamentals for Beginners to Advanced
CHAPTER 18 Beginner

Interfaces and Abstract Classes in C#

Updated: May 17, 2026
5 min read

# CHAPTER 18

Interfaces and Abstract Classes

1. Introduction

In the last chapter, we used Abstract Classes to define contracts. But what if a Smartphone class needs to inherit from Phone, Camera, and GPS? C# does NOT allow multiple class inheritance. To solve this, C# provides Interfaces. A class can inherit from only one Base Class, but it can implement *infinite* Interfaces!

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define and implement an Interface.
  • Differentiate between Interfaces and Abstract Classes.
  • Implement Multiple Interfaces in a single class.
  • Understand the concept of Dependency Abstraction.

3. What is an Interface?

An Interface is a completely abstract "blueprint of a blueprint." It defines *what* a class should do, but absolutely no details on *how* to do it.

By naming convention, Interfaces in C# always start with a capital I (e.g., IMovable, IDrawable).

csharp
123456789101112131415161718192021222324
using System;

// Defining an Interface
interface IPlayable
{
    // Interface methods have NO body and no access modifiers (they are public by default)
    void Play();
    void Pause();
}

// Implementing the Interface
class VideoPlayer : IPlayable
{
    // Must provide public implementations for all interface methods!
    public void Play()
    {
        Console.WriteLine("Playing Video...");
    }

    public void Pause()
    {
        Console.WriteLine("Video Paused.");
    }
}

4. Multiple Interface Implementation

This is where Interfaces shine. A class can implement multiple interfaces separated by commas.
csharp
12345678910111213141516
interface ICamera
{
    void TakePhoto();
}

interface IGPS
{
    void GetLocation();
}

// Inherits ONE Base class (Device), implements TWO Interfaces (ICamera, IGPS)
class SmartPhone : Device, ICamera, IGPS
{
    public void TakePhoto() { Console.WriteLine("Click!"); }
    public void GetLocation() { Console.WriteLine("Lat: 40, Long: -74"); }
}

5. Abstract Classes vs. Interfaces (The Golden Interview Question)

You will be asked this in every C# interview. Here is the difference:
FeatureAbstract ClassInterface
Multiple InheritanceA class can inherit only ONE abstract class.A class can implement MULTIPLE interfaces.
ImplementationCan contain fully written methods alongside abstract methods.Prior to C# 8, contained *only* abstract signatures. (C# 8 allows default implementations).
Fields/VariablesCan contain state (variables, fields).Cannot contain fields.
Access ModifiersCan use public, private, protected.Members are implicitly public.
PurposeDefines core Identity ("A Dog IS an Animal").Defines Capabilities ("A Dog CAN Walk").

6. Dependency Abstraction

Why are Interfaces so popular in enterprise software? Because they allow for Loose Coupling. If a function takes an ILogger interface as a parameter, you can pass it a ConsoleLogger, a FileLogger, or a CloudLogger. The function doesn't care *how* the logging works, as long as the object fulfills the ILogger contract!
csharp
12345678910111213
interface ILogger { void Log(string msg); }

class ConsoleLogger : ILogger { public void Log(string msg) { Console.WriteLine(msg); } }
class DatabaseLogger : ILogger { public void Log(string msg) { /* SQL Logic */ } }

class Application
{
    // Depends on the Interface, not the concrete class!
    public void Run(ILogger logger)
    {
        logger.Log("App Started!");
    }
}

7. Common Mistakes

  • Adding access modifiers inside an Interface: In classic C#, writing public void Play(); inside an interface will cause an error. Just write void Play();.
  • Forgetting to implement all methods: If a class claims to implement IPlayable, it MUST contain code for every single method defined in IPlayable. The compiler will not let you compile otherwise.

8. Best Practices

  • Always prefix your Interface names with the letter I (e.g., IRepository, IUserService).
  • Interface names are often adjectives (IMovable, IComparable) because they define capabilities rather than core identities.

9. Exercises

  1. 1. Create an interface IShape with a double GetArea() method. Implement it in a Rectangle class.
  1. 2. Create an interface IFlyable and an interface ISwimmable. Create a Duck class that implements both.

10. MCQs with Answers

Question 1

By C# naming convention, what letter should start an Interface name?

Question 2

How many interfaces can a single C# class implement?

Question 3

How many Abstract Classes can a single C# class inherit from?

Q4. Can an Interface contain variables (fields)? a) Yes b) No Answer: b) No

Q5. Are interface methods implicitly public? a) Yes b) No, they are private by default Answer: a) Yes

Q6. If a class implements an interface, must it provide code for ALL methods in that interface? a) Yes b) No, only the ones it needs Answer: a) Yes

Q7. Which represents an "Is-A" relationship? a) Interface (e.g., IFlyable) b) Base Class (e.g., Animal) Answer: b) Base Class (e.g., Animal)

Q8. Which represents a "Can-Do" capability? a) Interface (e.g., IFlyable) b) Base Class (e.g., Animal) Answer: a) Interface (e.g., IFlyable)

Question 9

What feature introduced in C# 8.0 blurred the lines between interfaces and abstract classes?

Question 10

Why do enterprise applications use Interfaces heavily?

11. Interview Questions

  • Q: What is the difference between an Interface and an Abstract Class? Give 3 differences.
  • Q: Explain why C# prohibits multiple class inheritance, but allows multiple interface implementation (The "Diamond Problem").

12. Summary

Interfaces define a strict contract of capabilities without dictating implementation. They allow C# to overcome the single-inheritance limitation, enabling classes to adopt multiple behaviors. Understanding the difference between Abstract Classes (Identity) and Interfaces (Capabilities) is a hallmark of a professional C# developer.

13. Next Chapter Recommendation

Your code is now well-architected, but what happens when it encounters an unexpected error? In Chapter 19: Exception Handling, we will learn how to prevent our applications from crashing.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·