C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
.NET
ADO.NET
Android
ASP.NET
C#
Databases & DBA
Design Patterns & Practices
iOS
Java
OOP/OOD
SharePoint
Software Testing
Web Development
WPF
View All
12
Reply
Constructor is possible in abstract class ?
Rajesh Singh
10y
2.2k
1
Reply
Delete Row
Delete Column
Insert Link
×
Insert
Cancel
Embed YouTube Video
×
Width (%)
Height (%)
Insert
Cancel
Table Options
×
Rows
Columns
First row as header
Create Table
Insert Image
×
Selected file:
Alignment
Left
Center
Right
Select an image from your device to upload
Upload to Server
Cancel
Submit
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
vipin kv
9y
2
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
Subhashkumar Yadav
8y
1
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);}
Ashutosh Pandey
9y
1
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.
Keerthi Venkatesan
9y
1
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();} }
Sankar Sai
9y
1
yes.abstract class MyClass{public MyClass(int x){}}
Rajesh Singh
10y
1
Yes It is possible..
Akhilesh Tiwari
8y
0
yes
Manoj Kumar
8y
0
no
Hiren Parmar
9y
0
Abstract has private constructor.
Sunil Babu
9y
0
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.
Sankar Sai
9y
0
no
Mohsen Goodarzi
9y
0
hese static constructors are correct ? class A { statc intA() { } static A(int x,int y) { } static A(int x) { }...
his abstract class is correct ? abstract class A { public abstract void Disp(); public abstract void B() { } ...
Message