As we know that base constructor invoked first when we create an instance of child class. But if we create an instance of child class by parameterized constructor and base class has both default and parameterized constructor then which constructor of the base will be invoked first?
Short Answer
When you create an instance of a child class using a parameterized constructor, and the base class has both a default (parameterless) constructor and a parameterized constructor, the parameterized constructor of the base class will be invoked first if you explicitly call it using the base
keyword. If you don’t use the base
keyword, the default constructor of the base class will be invoked.
Detailed Explanation
In object-oriented programming (OOP), when you create an instance of a child class, the constructor of the base class is always called first. This ensures that the base class is properly initialized before the child class. However, if the base class has multiple constructors (e.g., a default constructor and a parameterized constructor), which one gets called depends on how you define the child class constructor.
Let’s break this down with an example in C#.
Example: Base Class with Default and Parameterized Constructors
Suppose you have a base class called Animal
with two constructors:
- A default constructor (parameterless).
- A parameterized constructor that takes a string parameter.
You also have a child class called Dog
that inherits from Animal
. The Dog
class has a parameterized constructor.
Here’s the code:
using System;
class Animal
{
// Default constructor
public Animal()
{
Console.WriteLine("Base class default constructor");
}
// Parameterized constructor
public Animal(string type)
{
Console.WriteLine($"Base class parameterized constructor with type: {type}");
}
}
class Dog : Animal
{
// Child class constructor
public Dog(string name) : base("Dog") // Explicitly calling the base class parameterized constructor
{
Console.WriteLine($"Child class constructor with name: {name}");
}
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Dog class
Dog myDog = new Dog("Buddy");
}
}
Output
Base class parameterized constructor with type: Dog
Child class constructor with name: Buddy
What’s Happening Here?
- Base Class Constructor Invocation:
- When you create an instance of the
Dog
class, the constructor of the Animal
base class is called first.
- In this example, the
Dog
constructor explicitly calls the parameterized constructor of the Animal
class using the base("Dog")
syntax. This is why the output shows the parameterized constructor of the base class being invoked.
- Child Class Constructor:
- After the base class constructor is executed, the child class (
Dog
) constructor is called.
- The child class constructor prints the name of the dog, which is provided as a parameter.
What If You Don’t Use the base
Keyword?
If you don’t explicitly call a base class constructor using the base
keyword, the default constructor of the base class will be invoked automatically. Here’s an example:
class Dog : Animal
{
// Child class constructor (no explicit base call)
public Dog(string name)
{
Console.WriteLine($"Child class constructor with name: {name}");
}
}
Output
Base class default constructor
Child class constructor with name: Buddy
In this case:
- The default constructor of the
Animal
class is called because no specific base constructor is invoked.
- The child class constructor is then executed.
Key Takeaways
- Base Class Constructor is Always Called First:
- When you create an instance of a child class, the base class constructor is always executed before the child class constructor.
- Default Constructor is Called Automatically:
- If you don’t explicitly call a base class constructor using the
base
keyword, the default constructor of the base class will be invoked.
- Explicitly Call a Base Constructor:
- Use the
base
keyword in the child class constructor to call a specific constructor of the base class.
Real-Life Analogy
Think of building a house:
- The base class is like the foundation of the house. You need to build the foundation first before constructing the walls and roof.
- The child class is like the walls and roof. They depend on the foundation to be stable.
- If you don’t specify how to build the foundation (default constructor), the builders will use a standard method. But if you want a specific type of foundation (parameterized constructor), you need to give clear instructions.
Conclusion
When creating an instance of a child class, the base class constructor is always invoked first. If the base class has multiple constructors, the one that gets called depends on whether you explicitly use the base
keyword in the child class constructor. By understanding this behavior, you can control how your classes are initialized and ensure that your code works as expected.