Wednesday, 15 February 2012

Why abstract class contain constructors even we don't create object?

Yes, abstract class can't be instated but contatins constructors and destructors why because derived class of that abstract class executation starts from abstract class constructor.
Note: If we don't create constructor to abstract class complier implictly creates one default constructor.

Ex:
    abstract class ParentClass
    {
        public ParentClass()
        {
            Console.WriteLine("this is Parent Constructor");      
        }
    }
this constructor is executed whenever we create object to derived class(child class) of the Parenet class.


 class ChildClass:ParentClass
    {
        static void Main()
        {
            ChildClass obj = new ChildClass();
            Console.WriteLine("so execution of derived class starts from absract class Constructors");
            Console.Read();
       
        }
    }
Conculsion: Executation of the Child class start from Parent class Constructor. so in .net frame work many abstract classes contatins constructors.



No comments:

Post a Comment