Complete C# Tutorial

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.

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! ๐Ÿš€

Leave a Comment

Share this Doc

Constructors vs Inheritance

Or copy link