Interfaces and Abstract Classes in C#
# CHAPTER 18
Interfaces and Abstract Classes
1. Introduction
In the last chapter, we used Abstract Classes to define contracts. But what if aSmartphone 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).
4. Multiple Interface Implementation
This is where Interfaces shine. A class can implement multiple interfaces separated by commas.5. Abstract Classes vs. Interfaces (The Golden Interview Question)
You will be asked this in every C# interview. Here is the difference:| Feature | Abstract Class | Interface |
|---|---|---|
| Multiple Inheritance | A class can inherit only ONE abstract class. | A class can implement MULTIPLE interfaces. |
| Implementation | Can contain fully written methods alongside abstract methods. | Prior to C# 8, contained *only* abstract signatures. (C# 8 allows default implementations). |
| Fields/Variables | Can contain state (variables, fields). | Cannot contain fields. |
| Access Modifiers | Can use public, private, protected. | Members are implicitly public. |
| Purpose | Defines 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 anILogger 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!
7. Common Mistakes
-
Adding access modifiers inside an Interface: In classic C#, writing
public void Play();inside an interface will cause an error. Just writevoid Play();.
-
Forgetting to implement all methods: If a class claims to implement
IPlayable, it MUST contain code for every single method defined inIPlayable. 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.
Create an interface
IShapewith adouble GetArea()method. Implement it in aRectangleclass.
-
2.
Create an interface
IFlyableand an interfaceISwimmable. Create aDuckclass that implements both.
10. MCQs with Answers
By C# naming convention, what letter should start an Interface name?
How many interfaces can a single C# class implement?
How many Abstract Classes can a single C# class inherit from?
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)
What feature introduced in C# 8.0 blurred the lines between interfaces and abstract classes?
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").