top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Constructor and Destructor invoking sequence with inheritance

0 votes
253 views

During inheritance, base class may also contain constructor and destructor. 
In this case if you create an instance for the derived class then base class constructor will also be invoked and when derived instance is destroyed then base destructor will also be invoked and the order of execution of constructors will be in the same order as their derivation and order of execution of destructors will be in reverse order of their derivation.

Example Program
 

using System;

namespace ProgramCall
{
    class Base1
    {
        public Base1()
        {
            Console.WriteLine("Base  Class  Constructor");
        }
        ~Base1()
        {
            Console.WriteLine("Base  Class  Destructor");
        }
    }
    class Derived1 : Base1
    {

        public Derived1()
        {

            Console.WriteLine("Derived  Class  Constructor");
        }
        ~Derived1()
        {
            Console.WriteLine("Derived  Class  Destructor");
        }
    }
    class ConstructorInheritance
    {
        static void create()
        {
            Derived1 obj = new Derived1();
        }


        static void Main()
        {
            
            create();
            
            GC.Collect();
            Console.Read();
        }
    }
}



Output

Base  Class  Constructor
Derived  Class  Constructor
Derived  Class  Destructor
Base  Class  Destructor

Note

During inheritance, if the base class contains only parameterized constructor, then derived class must contain a parameterized constructor even it doesn’t need one. 

In this case while creating parameterized constructor in derived class, you must specify the parameters required for derived class along with the parameters required for base class constructor and to pass arguments to base class constructor, use the keyword “base” at the end of parameterized constructor declaration in derived class preceded with “:”.

Example Program

 

using System;

namespace ProgramCall
{
    class Base2
    {
        int A, B;
        public Base2(int X, int Y)
        {
            A = X;
            B = Y;

        }
        public void PrintAB()
        {
            Console.Write("A  =  {0}\tB  =  {1}\t", A, B);
        }
    }
    class Derived2 : Base2
    {
        int C;

        public Derived2(int X, int Y, int Z)
            : base(X, Y)
        {

            C = Z;
        }
        public void Print()
        {
            PrintAB();
            Console.WriteLine("C  =  {0}", C);
        }
    }

    class BaseParamConstructor
    {

        static void Main()
        {

            Derived2 obj = new Derived2(10, 20, 30);
            obj.Print();
            Console.Read();
        }
    }
}


Output

A  =  10        B  =  20        C  =  30

posted Feb 10, 2017 by Shivaranjini

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

A special method of the class that will be automatically invoked when an instance of the class is created is called as constructor. 

Constructors can be classified into 5 types
 

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor
  4. Static Constructor
  5. Private Constructor

Default Constructor : A constructor without any parameters is called as default constructor. Drawback of default constructor is every instance of the class will be initialized to same values and it is not possible to initialize each instance of the class to different values.

Example for Default Constructor

Parameterized Constructor : A constructor with at least one parameter is called as parameterized constructor. Advantage of parameterized constructor is you can initialize each instance of the class to different values.


Example for Parameterized Constructor

using System;

namespace ProgramCall
{
    class Test1
    {
        //Private fields of class
        int A, B;

        //default Constructor
        public Test1()
        {
            A = 10;
            B = 20;
        }

        //Paremetrized Constructor
        public Test1(int X, int Y)
        {
            A = X;
            B = Y;
        }



        //Method to print
        public void Print()
        {

            Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }
        
                
    }


    class MainClass
    {

        static void Main()
        {

            Test1 T1 = new Test1();  //Default Constructor is called
            Test1 T2 = new Test1(80, 40); //Parameterized Constructor is called
          
            T1.Print();
            T2.Print();

            Console.Read();
          
        }
    }

}


Output

A  =  10        B  =  20
A  =  80        B  =  40

Copy Constructor : A parameterized constructor that contains a parameter of same class type is called as copy constructor. Main purpose of copy constructor is to initialize new instance to the values of an existing instance.

Example for Copy Constructor

using System;

namespace ProgramCall
{

    class Test2
    {

        int A, B;

        public Test2(int X, int Y)
        {
            A = X;
            B = Y;
        }

        //Copy Constructor
        public Test2(Test2 T)
        {
            A = T.A;

            B = T.B;
        }


       
        public void Print()
        {

            Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);
        }
     

    }


    class CopyConstructor
    {
        static void Main()
        {
           

            Test2 T2 = new Test2(80, 90);

            //Invoking copy constructor
            Test2 T3 = new Test2(T2); 
            
            T2.Print();
            T3.Print();

            Console.Read();
        }
    }
}



Output

A  =  80        B  =  90
A  =  80        B  =  90

Static Constructor : You can create a constructor as static and when a constructor is created as static, it will be invoked only once for any number of instances of the class and it is during the creation of first instance of the class or the first reference to a static member in the class. Static constructor is used to initialize static fields of the class and to write the code that needs to be executed only once.

Example for Static Constructor

using System;

namespace ProgramCall
{
    class Test3
    {

        public Test3()
        {

            Console.WriteLine("Instance  Constructor");
        }

        static Test3()
        {
            Console.WriteLine("Static  Constructor");
        }
    }
    class StaticConstructor
    {
        static void Main()
        {
            //Static Constructor and instance constructor, both are invoked for first instance.
            Test3 T1 = new Test3(); 

            //Only instance constructor is invoked.
            Test3 T2 = new Test3(); 
     

            Console.Read();
        }
    }
}



Output

Static  Constructor
Instance  Constructor
Instance  Constructor

Private Constructor You can also create a constructor as private. When a class contains at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated when it contains every member as static. 

Some unique points related to constructors are as follows

  • A class can have any number of constructors.
  • A constructor doesn’t have any return type even void.
  • A static constructor can not be a parameterized constructor.
  • Within a class you can create only one static constructor.
READ MORE

  The following example program in c# implements multiple inheritance in .net with interfaces. Multiple inheritance in .NET framework cannot be implemented with classes, It can only be implemented with interfaces.
 

Example program for Multiple Inheritance


 

using System;

 

//Example Program for Multiple Inheritance

namespace ProgramCall

{

 

    //The Icar interface should defines all car properties

    interface Icar

    {

        int WheelsCount();

 

    }

 

 

 

    //The IPlane interface should defines all plane properties

    interface IPlane

    {

        bool CanFly

        {

            get;

        }

 

    }

 

 

 

    //The superCar class should implement Icar and Iplane interfaces to become supercar

    class SuperCar : IcarIPlane

    {

 

        //The class should implement both intefaces

        #region Icar Members

 

        public int WheelsCount()

        {

            return 4;

        }

 

        #endregion

 

 

 

 

        #region IPlane Members

 

        public bool CanFly

        {

            get

            {

                return true;

            }

 

        }

 

        #endregion

    }

 

 

 

 

 

    class Program

    {

        static void Main(string[] args)

        {

            //Creating instance for SuperCar class

            SuperCar mysupercar = new SuperCar();

 

            Console.WriteLine("My Super Car has " + mysupercar.WheelsCount() + " Wheels and can fly is " + mysupercar.CanFly);

 

            Console.ReadLine();

        }

 

    }

}

 

 

//Output

//My Super Car has 4 Wheels and can fly is True


 Output 
My Super Car has 4 Wheels and can fly is True

READ MORE
...