Let's create two classes: Base class and Derived class. Derived Class inherits from the Base class. We have Base () and Derived () constructor for Base class and Derived class respectively.
Now when we create a Derived class object Base constructor gets executed, Let's see in the code:
Code
- using System;
- namespace OOPS
- {
- class Program
- {
- static void Main(string[] args)
- {
- Derived d = new Derived();
- Console.ReadLine();
- }
- }
-
- class Base
- {
-
- public Base()
- {
- Console.WriteLine(" I am Base class constructor");
- }
- }
-
- class Derived :Base
- {
-
- public Derived()
- {
- Console.WriteLine(" I am Derived class constructor");
- }
- }
- }
Output So In this example we saw the Base Constructor is executed first when creating a derived class object. Hope you got the concept ,Thanks for reading.