13. Abstract Classes in C#
Level: Beginner
- What an abstract class is
- Why abstract classes are useful
- What an abstract method is
- How abstract classes differ from interfaces at a high level
- Use abstract classes in School Management examples
In the previous article, you learned inheritance.
Now we learn an important design tool: abstract class.
Do not worry. We will keep it simple.
The Problem: We Need a Common Rule
In a school system:
- Student is a school member
- Teacher is a school member
- Admin is a school member
All school members have common data:
- Name
- IsActive
But each member shows information differently.
This is where abstract class helps.
Quick Definitions
- Abstract class - Incomplete parent class, cannot create object directly
- Abstract method - Method without body, child must implement it
- Contract - Promise that class will provide certain methods
- Interface - Set of methods a class must implement
- Implementation - Writing the body of an abstract method
- Capability - Ability a class has (like "printable", "payable")
- Multiple inheritance - Class can implement many interfaces
- Is-a relationship - Student IS-A SchoolMember (abstract class)
- Can-do relationship - FeeReceipt CAN-DO Print (interface)
What is an Abstract Class?
An abstract class is an incomplete parent class.
You cannot create an object directly from it.
It is meant to be inherited by child classes.
public abstract class SchoolMember
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public abstract void PrintInfo();
}
PrintInfo() has no body.
That means every child class must write its own PrintInfo().
Why Abstract Classes? (The "Contract" Idea)
Imagine parent class says:
"All school members must be able to PrintInfo().
But I don't know HOW each one prints info.
Student prints one way. Teacher prints another way.
So child classes, YOU figure out how to PrintInfo()."
This is the abstract method contract.
- Parent: "You must have this method."
- Child: "OK, here's my version."
Real SMS benefit:
- Guarantee every SchoolMember can PrintInfo()
- Each prints their own way
- Cannot create a generic "SchoolMember" object (too vague)
- Must create Student or Teacher object (specific)
Step 1: Abstract Class with Shared Data
public abstract class SchoolMember
{
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public bool IsActive { get; set; } = true;
public void PrintStatus()
{
Console.WriteLine(IsActive ? "Active" : "Inactive");
}
public abstract void PrintInfo();
}
This class has:
- Normal properties
- Normal method
PrintStatus - Abstract method
PrintInfo
Step 2: Student Implements Abstract Method
public class Student : SchoolMember
{
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";
public override void PrintInfo()
{
Console.WriteLine($"Student: {Name}");
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Class: {ClassName}");
}
}
Because SchoolMember has abstract PrintInfo(), Student must implement it.
Step 3: Teacher Implements Abstract Method
public class Teacher : SchoolMember
{
public string EmployeeCode { get; set; } = "";
public string Subject { get; set; } = "";
public override void PrintInfo()
{
Console.WriteLine($"Teacher: {Name}");
Console.WriteLine($"Employee Code: {EmployeeCode}");
Console.WriteLine($"Subject: {Subject}");
}
}
Full Example: Abstract Class
Quick Preview: Abstract Class vs Interface
An interface is a contract.
It says:
Any class that uses this interface must provide these methods.
Example:
public interface IPrintable
{
void Print();
}
Any class that implements IPrintable must have Print().
Small Interface Preview
public interface IPrintable
{
void Print();
}
public class FeeReceipt : IPrintable
{
public string StudentName { get; set; } = "";
public decimal Amount { get; set; }
public void Print()
{
Console.WriteLine($"Receipt: {StudentName} paid {Amount}");
}
}
Abstract Class vs Interface
| Question | Abstract Class | Interface |
|---|---|---|
| Can have shared code? | Yes | Usually no shared state |
| Can create object directly? | No | No |
| Relationship | Is-a | Can-do |
| Example | Student is a SchoolMember | FeeReceipt can print |
| Multiple allowed? | One base class only | Many interfaces allowed |
Simple rule:
Use abstract class when classes share common code.
Use interface when classes share a capability.
When You'll Use This in SMS
Abstract classes organize school entities.
SchoolMember (abstract base)
|-- Student (override PrintInfo)
|-- Teacher (override PrintInfo)
+-- Staff (override PrintInfo)
All share: Name, Email, IsActive, PrintStatus()
Each prints differently in PrintInfo().
Interfaces define capabilities.
IPrintable -> FeeReceipt prints receipt
IPayable -> FeeAccount handles payment
IExportable -> Report exports to CSV
Real impact:
- Cannot create
SchoolMember()directly (too vague) - Must create
Student()orTeacher()(specific) - All have PrintInfo(), but each behaves correctly
- Different classes can implement same interface independently
- Production SMS has dozens of abstract classes and interfaces
Try This Now
Run the abstract class example above. Then experiment:
- Try creating:
SchoolMember member = new SchoolMember();(should error) - Change abstract method name to
PrintRole() - Override in both Student and Teacher
- Create multiple SchoolMember variables pointing to Student/Teacher objects
- Call PrintInfo() on each and see different output
See how abstract forces child classes to implement their own version.
Abstract classes video coming soon on NexCoding YouTube. Subscribe for updates.
Common Mistakes
Mistake 1: Creating object from abstract class
Wrong:
SchoolMember member = new SchoolMember();
Correct:
SchoolMember member = new Student();
Mistake 2: Not implementing abstract method
If parent has abstract method, child must override it.
Mistake 3: Confusing abstract class and interface
Use abstract class for shared base code. Use interface for capability.
Mistake 4: Interface name without I
Prefer:
public interface IPrintable
Best Practices
- Keep abstract classes simple.
- Use abstract methods only when every child must implement them.
- Use interface names starting with
I. - Use interfaces for abilities like
IPrintable,IPayable. - Do not overuse abstract classes as a beginner.
Practice Task
Create:
- Abstract class
SchoolDocument - Property
Title - Abstract method
Print() - Class
ReportCardinheritingSchoolDocument - Class
FeeReceiptinheritingSchoolDocument
Each child class should print differently.
Quick Revision
| Question | Answer |
|---|---|
| What is abstract class? | Incomplete parent class |
| Can abstract class object be created? | No |
| What is abstract method? | Method without body |
| Who implements abstract method? | Child class |
| What is interface? | Contract/capability |
| Interface naming convention? | Starts with I |
An abstract class is an incomplete parent class. It is used as a base class and cannot be instantiated directly.
An abstract method has no body in the parent class. Child classes must override and implement it.
An interface is a contract. A class implementing it must provide the required methods.
Abstract class is used for shared base code. Interface is used for shared capability or contract.
Yes.
public class Student : SchoolMember, IPrintable
{
}
Use ChatGPT, Claude, or Copilot to go deeper on C# abstract classes and interfaces. Try these prompts:
"Explain abstract class and interface using school examples""Give me 5 practice tasks for abstract classes""Explain abstract class vs interface simply""Quiz me with 5 beginner questions"
💡 Tip: After reading this article, paste your own code into AI and ask "What could go wrong here and why?" — fastest way to find edge cases and deepen understanding.