Skip to main content

13. Abstract Classes in C#

Level: Beginner

ℹ️ What You'll Learn
  • 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
  • Email
  • 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

💻 Try It — Console App
💡 Paste into Program.cs and press F5⌥ GitHub
SchoolMember member1 = new Student
{
Name = "Sahasra Kumar",
Email = "Sahasra@school.com",
RollNumber = 101,
ClassName = "10-A"
};

SchoolMember member2 = new Teacher
{
Name = "Meena Rao",
Email = "meena@school.com",
EmployeeCode = "EMP501",
Subject = "Mathematics"
};

member1.PrintInfo();
member1.PrintStatus();

member2.PrintInfo();
member2.PrintStatus();

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");
Console.WriteLine();
}

public abstract void PrintInfo();
}

public class Student : SchoolMember
{
public int RollNumber { get; set; }
public string ClassName { get; set; } = "";

public override void PrintInfo()
{
Console.WriteLine("=== Student ===");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Roll Number: {RollNumber}");
Console.WriteLine($"Class: {ClassName}");
}
}

public class Teacher : SchoolMember
{
public string EmployeeCode { get; set; } = "";
public string Subject { get; set; } = "";

public override void PrintInfo()
{
Console.WriteLine("=== Teacher ===");
Console.WriteLine($"Name: {Name}");
Console.WriteLine($"Employee Code: {EmployeeCode}");
Console.WriteLine($"Subject: {Subject}");
}
}

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

QuestionAbstract ClassInterface
Can have shared code?YesUsually no shared state
Can create object directly?NoNo
RelationshipIs-aCan-do
ExampleStudent is a SchoolMemberFeeReceipt can print
Multiple allowed?One base class onlyMany 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() or Teacher() (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:

  1. Try creating: SchoolMember member = new SchoolMember(); (should error)
  2. Change abstract method name to PrintRole()
  3. Override in both Student and Teacher
  4. Create multiple SchoolMember variables pointing to Student/Teacher objects
  5. Call PrintInfo() on each and see different output

See how abstract forces child classes to implement their own version.


ℹ️ Video Tutorial

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

  1. Keep abstract classes simple.
  2. Use abstract methods only when every child must implement them.
  3. Use interface names starting with I.
  4. Use interfaces for abilities like IPrintable, IPayable.
  5. Do not overuse abstract classes as a beginner.

Practice Task

Create:

  • Abstract class SchoolDocument
  • Property Title
  • Abstract method Print()
  • Class ReportCard inheriting SchoolDocument
  • Class FeeReceipt inheriting SchoolDocument

Each child class should print differently.

Quick Revision

QuestionAnswer
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

🎯 Q1: What is an abstract class?

An abstract class is an incomplete parent class. It is used as a base class and cannot be instantiated directly.

🎯 Q2: What is an abstract method?

An abstract method has no body in the parent class. Child classes must override and implement it.

🎯 Q3: What is an interface?

An interface is a contract. A class implementing it must provide the required methods.

🎯 Q4: Difference between abstract class and interface?

Abstract class is used for shared base code. Interface is used for shared capability or contract.

🎯 Q5: Can a class inherit abstract class and implement interface?

Yes.

public class Student : SchoolMember, IPrintable
{
}

🤖Use AI to Learn Faster
⚠️ Important for beginners: Do NOT use AI to write your code yet. Type every example yourself. Your brain learns by doing, not by reading AI output. Use AI only to explain and quiz you — not to code for you. Once you have strong fundamentals, AI becomes a powerful productivity tool for repetitive tasks.

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.

Next Article

Interfaces in C# ->

nexcoding.in