Friday, 26 August 2011

Order of Constructor execution in Inheritance

  •  constructors are called in the order from the top to the bottom (parent to child class) in inheritance hierarchy.
See the below program and check the output ...




class parent
    {
        public parent()
        {
            Console.WriteLine("This is Paarent");
        }
    }
    class child : parent
    {
        public child()
        {
            Console.WriteLine("This is child");
        }
    }

    class Program : child
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.Read();

        }

        public Program()
        {
            Console.WriteLine("This is program");
        }
    }


Output:

This is Paarent
This is child
This is program

No comments:

Post a Comment