Why Abstract Class has Constructors

Do U hav the Following Questions
  • How Abstract Class can be instantiated?
  • we can't instantiate abstract classes then what is the use of constructors in it?

Here is Answer For it
  • abstract class constructors are called from the derived class constructors
  • use base(arg list); for it
abstract class MyClass
{
    int variable;
    public MyClass(int variable)
    {
        this.variable = variable;
        Console.WriteLine("abstract class variable={0}",variable);
    }
    public abstract void aMethod();
    public int Variable
    {
        get
        {
            return Variable;
        }
        set
        {
            Variable = value;
        }
    }
}

class MySubClass : MyClass
{
    int variable;
    //calls the base class contructor
    MySubClass (int variable):base(variable)
    {
        this.variable=variable;
        Console.WriteLine("MySubClass constructor aVar={0}",variable);
    }
    static void Main()
    {
        MySubClass sc = new MySubClass(100);
        Console.WriteLine(sc.Variable.ToString());
    }
}
Thanks for reading - your comments encourage me, Rama Krishna Chunduri
Share