12
Reply

Constructor is possible in abstract class ?

Rajesh Singh

Rajesh Singh

9y
2.2k
1
Reply

    Yes, it is possible. Unlike interfaces, abstract classes do contain implementation.So to initialize its members we can use the constructor and the abstract class constructor will invoke prior to derived class constructor

    Yes surely you can add one, as already mentioned for initialization of Abstract class variables. BUT if you dont explicitly declare one, it anyways has an implicit constructor for "Constructor Chaining" to work. Abstract class can have a constructor though it cannot be instantiated

    Yeah..We can have the constructor in abstract class, below are the example-abstract class A{public int a;public A(){a = 10;} }class B : A{public int b;public B(){b = 20;}}protected void Page_Load(object sender, EventArgs e){B b1 = new B(); Response.Write(b1.a);Response.Write(b1.b);}

    You can create constructors in an abstract class - but they are not inherited. Your only choice in the derived class is which of the base class constructors you will call. You cannot declare an abstract constructor, because constructors cannot be overridden, regardless of whether the class is abstract or can be instantiated.In the abstract class, you just declare constructors as normal.

    using System; abstract class absclass {public absclass(){Console.WriteLine("I am abstract class constructor");}public abstract void Print(); } class norClass : absclass {public norClass(){Console.WriteLine("I am derived classs constructor");}public override void Print(){Console.WriteLine("I am print method");} } class mainclass {static void Main() {norClass obj = new norClass();} }

    yes.abstract class MyClass{public MyClass(int x){}}

    Yes It is possible..

    yes

    no

    Abstract has private constructor.

    Yes it is possible..We can point a abstract class object to a derived class. While performing this action by default abstract class constructor will get executed first. Also the legend theory of inheritance gives the answer here, parent class constructor gets executed before child class constructor. If we are using abstract class we need to implement it on derived class.

    no