Abstract Classes in C# – A Beginner’s Guide
👋 Introduction to Abstract Classes in C#
Ever heard of a class that cannot be instantiated? That’s exactly what an abstract class is! In Object-Oriented Programming (OOP), an abstract class serves as a blueprint for other classes. It can contain abstract methods (without implementation) and regular methods (with implementation).
For example, imagine a Vehicle class. You know every vehicle has wheels, but the number of wheels differs (bike has 2, car has 4). So, we can declare an abstract class Vehicle with an abstract method NumberOfWheels(), which different vehicles will implement in their own way.
Sounds interesting? Let’s dive in! 🚀
📚 What You Will Learn in This Lesson?
✔️ What are Abstract Classes in C#?
✔️ Why and when should we use them?
✔️ How to declare and use abstract methods.
✔️ Real-world examples of abstract classes.
✔️ Hands-on coding examples.
🧐 What is an Abstract Class in C#?
An abstract class is a class that cannot be instantiated (you cannot create an object of it). It may contain abstract methods (methods without a body) and regular methods (with a body).
💡 Key Points:
- An abstract class is declared using the
abstract
keyword. - It can have abstract and non-abstract methods.
- Abstract methods must be implemented in derived classes.
✍ Syntax of Abstract Class in C#
abstract class ClassName
{
public abstract void AbstractMethod(); // Abstract method (no body)
public void RegularMethod()
{
Console.WriteLine("This is a regular method.");
}
}
⚡ Example 1: Basic Abstract Class in C#
using System;
abstract class Animal // Abstract class
{
public abstract void MakeSound(); // Abstract method
}
class Dog : Animal // Derived class
{
public override void MakeSound()
{
Console.WriteLine("Dog barks: Woof Woof!");
}
}
class Program
{
static void Main()
{
Animal myDog = new Dog(); // Creating object of derived class
myDog.MakeSound();
}
}
✅ Output:
Dog barks: Woof Woof!
📌 Explanation:
Animal
is an abstract class that contains an abstract methodMakeSound()
.Dog
is a derived class that overridesMakeSound()
.- We create an object of Dog using the base class reference (Animal myDog = new Dog()).
- When we call
MakeSound()
, it executesDog
’s version.
🏆 Example 2: Abstract Class with Concrete Methods
using System;
abstract class Vehicle
{
public abstract void StartEngine(); // Abstract method
public void Honk()
{
Console.WriteLine("Beep! Beep!"); // Regular method
}
}
class Car : Vehicle
{
public override void StartEngine()
{
Console.WriteLine("Car engine started!");
}
}
class Program
{
static void Main()
{
Car myCar = new Car();
myCar.StartEngine(); // Calls overridden method
myCar.Honk(); // Calls inherited method
}
}
✅ Output:
Car engine started!
Beep! Beep!
📌 Key Takeaway:
- Abstract classes can have both abstract and concrete (regular) methods.
- The
Honk()
method is inherited without overriding.
🌎 Real-World Example: Abstract Class for Payment Methods
Imagine an e-commerce website where users can pay using Credit Card, PayPal, or Bitcoin. The payment method differs, but all payments process transactions.
🛠 Code Example:
using System;
abstract class Payment
{
public abstract void ProcessPayment(double amount);
public void TransactionSuccessful()
{
Console.WriteLine("Transaction Completed!");
}
}
class CreditCard : Payment
{
public override void ProcessPayment(double amount)
{
Console.WriteLine($"Paid {amount} using Credit Card.");
}
}
class PayPal : Payment
{
public override void ProcessPayment(double amount)
{
Console.WriteLine($"Paid {amount} using PayPal.");
}
}
class Program
{
static void Main()
{
Payment payment1 = new CreditCard();
payment1.ProcessPayment(100);
payment1.TransactionSuccessful();
Payment payment2 = new PayPal();
payment2.ProcessPayment(50);
payment2.TransactionSuccessful();
}
}
✅ Output:
Paid 100 using Credit Card.
Transaction Completed!
Paid 50 using PayPal.
Transaction Completed!
🎯 Why is this Important?
- Common functionality (
TransactionSuccessful()
) is inherited. - Different payment methods implement their own
ProcessPayment()
. - This ensures flexibility and code reusability.
❓ Why Use Abstract Classes in C#?
➡️ When you want to enforce a contract for derived classes.
➡️ To define common behavior while allowing flexibility in implementation.
➡️ To follow the Open-Closed Principle (OCP) in SOLID design.
➡️ To organize code better in large applications.
🎯 Conclusion
Abstract classes in C# are powerful tools in object-oriented programming. They help create structured, reusable, and flexible code. With abstract methods, we enforce mandatory implementation in child classes. We also saw how abstract classes improve real-world applications like payment processing.
🚀 Next What?
In the next chapter, you will learn Abstract Methods in C# – where we dive deeper into how abstract methods work and when to use them! Stay tuned! 🎯
Now, it’s your turn! Try creating your own abstract class and implement different derived classes. Happy coding! 🎉🚀