Rajesh Singh
Constructor is possible in abstract class ?
By Rajesh Singh in C# on Nov 16 2015
  • vipin kv
    Mar, 2016 10

    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

    • 2
  • Subhashkumar Yadav
    Dec, 2016 21

    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

    • 1
  • Ashutosh Pandey
    Jun, 2016 13

    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);}

    • 1
  • Keerthi Venkatesan
    Mar, 2016 31

    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.

    • 1
  • Sankar Sai
    Feb, 2016 17

    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();} }

    • 1
  • Rajesh Singh
    Nov, 2015 16

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

    • 1
  • Akhilesh Tiwari
    Jan, 2017 4

    Yes It is possible..

    • 0
  • Manoj Kumar Duraisamy
    Jan, 2017 3

    yes

    • 0
  • Hiren Parmar
    Aug, 2016 15

    no

    • 0
  • Sunil Babu
    Apr, 2016 2

    Abstract has private constructor.

    • 0
  • Sankar Sai
    Feb, 2016 17

    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.

    • 0
  • Mohsen Goodarzi
    Feb, 2016 2

    no

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS