Constuctor
what will happen if i use constructor in abstract class in asp.net
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Akash Roy4 reputationPosted Nov 7, 2012, 2:49 AM EST
That way you can have classes that inherit from that class call the base constructor.
public abstract class A{
private string data;
protected A(string myString){
data = myString;
}
}
public class B : A {
B(string myString) : base(mystring){}
}
accept this answer if it solves your issue..
Akash
VulpesRank #55 · 29.2K reputationPosted Nov 7, 2012, 3:44 AM EST
If you change A's constructor from protected to public in Akash's example and try to instantiate A using this line:
A a = new A("Hello");
then you'll get the following compiler error:
Akash Roy4 reputationPosted Nov 7, 2012, 9:13 PM EST
VulpesRank #55 · 29.2K reputationPosted Nov 8, 2012, 1:15 AM EST
There's nothing wrong with your example which is a good one :)