Let's begin with a simple base class and derived class, and explain how the both are working.
Let's create a Base Class with the constructor taking no parameters,
class BaseClass1
{
public BaseClass1()
{
Console.WriteLine("In BaseClass1 constructor");
}
}
And Creating a Derived Class DerivedClass1 which inherits Base class BaseClass1
class DerivedClass1 : BaseClass1
{
public DerivedClass1()
{
Console.WriteLine("In DerivedClass1 constructor");
}
}
And in the Main Function just create an object for the BaseClass1
BaseClass1 BC1Obj = new BaseClass1();
Now the output will be
In BaseClass1 constructor
While trying to create another object for
BaseClass1 BC1Obj2 = new DerivedClass1();
Now the output will be
In BaseClass1 constructor
In DerivedClass1 constructor
While trying to create another object for
DerivedClass1 DC1Obj1 = new DerivedClass1();
Now the output will be
In BaseClass1 constructor
In DerivedClass1 constructor
This is the order of execution of constructors if we are using inheritance,
A detailed video has been posted in Youtube with the Examples:
Source Code Link : https://github.com/oneananda/C_Sharp_Examples/blob/main/OOP_Examples_1/Program.cs
Let's see in the next part
Comments