Constructors in Inheritance in C# โ Easy Guide with Examples
๐ Introduction
Hey there! Have you ever wondered how constructors work in inheritance? If yes, then you are in the right place! Constructors in Inheritance in C# help us initialize objects properly when working with base and derived classes. But sometimes, things can get a little confusing! Don’t worryโIโll make this super easy and fun for you.
๐ What You Are Going to Learn in This Lesson
โ๏ธ What are Constructors in Inheritance in C#?
โ๏ธ Why are they important?
โ๏ธ How constructors work in base and derived classes
โ๏ธ How to use base
keyword to call parent class constructor
โ๏ธ Real-world example to understand better
Let’s start with a simple example:
class Animal
{
public Animal()
{
Console.WriteLine("Animal Constructor Called");
}
}
class Dog : Animal
{
public Dog()
{
Console.WriteLine("Dog Constructor Called");
}
}
class Program
{
static void Main()
{
Dog d = new Dog();
}
}
Output:
Animal Constructor Called
Dog Constructor Called
See that? The base class constructor runs first! But why? Keep reading!
๐ฅ Why Are Constructors in Inheritance in C# Important?
When you inherit a class, the base class constructor must run first. This ensures that everything from the parent class gets set up before the child class does its own thing. Without this, things could break!
For example, imagine a “Vehicle” class setting up an engine. The “Car” class that inherits it should have the engine ready before adding features like air conditioning, right? That’s why base class constructors are called first!
๐ฏ How Do Constructors Work in Inheritance?
Whenever we create an object of a derived class, these things happen:
1๏ธโฃ The base class constructor runs first.
2๏ธโฃ Then, the derived class constructor runs.
By default, C# automatically calls the base class constructor. But we can also control this using the base
keyword.
๐ป Syntax:
class BaseClass
{
public BaseClass()
{
Console.WriteLine("Base Constructor Called");
}
}
class DerivedClass : BaseClass
{
public DerivedClass()
{
Console.WriteLine("Derived Constructor Called");
}
}
๐ Example 1: Default Constructor in Inheritance
Let’s see a program that demonstrates how constructors work in inheritance.
class Parent
{
public Parent()
{
Console.WriteLine("Parent Constructor is Called");
}
}
class Child : Parent
{
public Child()
{
Console.WriteLine("Child Constructor is Called");
}
}
class Program
{
static void Main()
{
Child obj = new Child();
}
}
Output:
Parent Constructor is Called
Child Constructor is Called
Did you notice? The parent constructor is always called first!
๐ Example 2: Using base
to Call a Parameterized Constructor
What if the base class has a parameterized constructor? You need to use the base
keyword to explicitly call it.
class Person
{
public Person(string name)
{
Console.WriteLine($"Hello, {name}!");
}
}
class Employee : Person
{
public Employee(string name) : base(name)
{
Console.WriteLine("Employee Constructor Called");
}
}
class Program
{
static void Main()
{
Employee emp = new Employee("Alice");
}
}
Output:
Hello, Alice!
Employee Constructor Called
So, the base
keyword lets us pass values to the base class constructor. Cool, right?
๐ Example 3: Real-World Scenario
Imagine a Bank Account System.
- The BankAccount class initializes the account with a balance.
- The SavingsAccount class adds interest rate functionality.
class BankAccount
{
public double Balance;
public BankAccount(double balance)
{
Balance = balance;
Console.WriteLine($"Account Created with Balance: {balance}");
}
}
class SavingsAccount : BankAccount
{
public double InterestRate;
public SavingsAccount(double balance, double interestRate) : base(balance)
{
InterestRate = interestRate;
Console.WriteLine($"Savings Account Created with Interest Rate: {interestRate}%");
}
}
class Program
{
static void Main()
{
SavingsAccount acc = new SavingsAccount(1000, 5);
}
}
Output:
Account Created with Balance: 1000
Savings Account Created with Interest Rate: 5%
Now that makes sense, right? The base constructor initializes the balance, and the derived constructor sets up the interest rate!
๐ Conclusion
โ
The base class constructor always runs first.
โ
Use base
to call a specific base constructor.
โ
Constructors in Inheritance in C# ensure proper initialization.
โ
This helps create well-structured, real-world applications.
ย
โญ๏ธ Next What?
Great job! You now understand Constructors in Inheritance in C#! ๐ But wait, thereโs more! In the next lesson, youโll learn about Sealed Classes and Methodsโwhich stop further inheritance! Sounds interesting? Stay tuned! ๐