Abstract Classes with Constructors in C# - Learn with Examples
🎯 Introduction
Hey there! 👋 Have you ever wondered if abstract classes in C# can have constructors? The answer is YES! But wait… why would you need a constructor in an abstract class when you can’t create its object? 🤔
That’s exactly what we’re going to cover today! Let’s break it down step by step and make it super easy to understand.
📚 What You Are Going to Learn in This Lesson
✔️ What are Abstract Classes with Constructors in C#?
✔️ Why do we need them?
✔️ How to use constructors in abstract classes?
✔️ Real-world examples
✔️ Code samples with explanation
🧐 What is an Abstract Class with a Constructor?
An abstract class is a class that cannot be instantiated (meaning, you can’t create its object). However, it can have a constructor! But why?
👉 Purpose: The constructor in an abstract class is used to initialize common fields before a derived (child) class is created.
Think of it like setting up a blueprint that ensures some values are initialized before any subclass uses them.
🛠️ Syntax of Abstract Class with Constructor
Here’s how you define a constructor inside an abstract class:
abstract class Animal
{
protected string name;
// Constructor in Abstract Class
public Animal(string name)
{
this.name = name;
Console.WriteLine("Animal Constructor Called");
}
public abstract void MakeSound();
}
➡️ The constructor Animal(string name)
initializes the name
field.
➡️ Child classes will call this constructor using base
keyword.
🎯 Simple Example of Abstract Class with Constructor
using System;
abstract class Animal
{
protected string name;
// Constructor in Abstract Class
public Animal(string name)
{
this.name = name;
Console.WriteLine($"Animal Constructor: {name} is created");
}
public abstract void MakeSound();
}
class Dog : Animal
{
public Dog(string name) : base(name)
{
Console.WriteLine("Dog Constructor Called");
}
public override void MakeSound()
{
Console.WriteLine($"{name} says Woof! 🐶");
}
}
class Program
{
static void Main()
{
Dog myDog = new Dog("Buddy");
myDog.MakeSound();
}
}
🔍 Output
Animal Constructor: Buddy is created
Dog Constructor Called
Buddy says Woof! 🐶
🛠️ Explanation
1️⃣ The abstract class Animal
has a constructor that initializes the name
field.
2️⃣ The child class Dog
inherits from Animal
and calls the base constructor using base(name)
.
3️⃣ When we create a Dog
object, both constructors (Animal → Dog) get executed.
💡 Key Takeaway: Abstract class constructors are executed first before the child class constructor.
🏢 Real-World Example
Let’s say we have different types of bank accounts like SavingsAccount and CurrentAccount. Each account has an account number, so it makes sense to initialize it in an abstract class.
using System;
abstract class BankAccount
{
protected string accountNumber;
public BankAccount(string accNumber)
{
this.accountNumber = accNumber;
Console.WriteLine($"Bank Account {accountNumber} Created ✅");
}
public abstract void DisplayAccountType();
}
class SavingsAccount : BankAccount
{
public SavingsAccount(string accNumber) : base(accNumber)
{
Console.WriteLine("Savings Account Created");
}
public override void DisplayAccountType()
{
Console.WriteLine($"Account {accountNumber} is a Savings Account 🏦");
}
}
class Program
{
static void Main()
{
SavingsAccount myAccount = new SavingsAccount("123456789");
myAccount.DisplayAccountType();
}
}
🔍 Output
Bank Account 123456789 Created ✅
Savings Account Created
Account 123456789 is a Savings Account 🏦
💡 Here, the constructor of BankAccount
ensures every account gets an account number before creating a SavingsAccount
.
🛠️ Why Are Abstract Class Constructors Important?
1️⃣ Code Reusability: Avoid repeating the same initialization code in every child class.
2️⃣ Encapsulation: Hide the initialization logic inside the abstract class.
3️⃣ Maintainability: If initialization logic changes, you only update it in one place!
🏁 Conclusion
So, we just unlocked the power of constructors inside abstract classes! 🚀 Now, you know:
✅ Abstract classes can have constructors!
✅ These constructors are executed before the child class constructors.
✅ They help initialize shared data across child classes.
✅ They improve code reusability and maintainability.
🔄 Next What?
In the next chapter, we will learn about Delegates and Events in C#! 🎉
So, are you excited to explore how C# handles event-driven programming? Stay tuned! 🚀