Why we cant use the abstract constructor ? What it is the reasons ?
If we will use what will be happen ?
Please clarify me ? give some examples ?
Loading
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
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2013, 3:44 AM EDT
The output is:
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2013, 1:33 AM EDT
However, this doesn't mean that an abstract class can't have a constructor - it can - and you can call it from a derived class constructor using the ' : base ' syntax.
For example:
using System;
abstract class Abstract
{
protected Abstract(string message)
{
Console.WriteLine(message);
}
}
class Derived : Abstract
{
public Derived(string message) : base(message)
{
}
}
class Program
{
static void Main()
{
Abstract ab = new Derived("Hello");
Console.ReadKey();
}
}
If you don't give an abstract class a constructor, then the system provides it with a protected parameterless constructor which does nothing except call its base class's parameterless constructor.
Vijay134 reputationPosted Aug 19, 2013, 2:29 AM EDT
we can create like this?
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2013, 2:57 AM EDT
You couldn't do it here anyway because the constructor is protected but even if you made it public, the compiler still wouldn't allow you to call it directly. It would generate the following error:
However, calling it via a derived class constructor is no problem. When you do this you're not creating an instance of the abstract class as such. You're just laying the groundwork for creating an object of the 'concrete' Derived class.
Vijay134 reputationPosted Aug 19, 2013, 3:19 AM EDT
Jignesh TrivediRank #16 · 50.3K reputationPosted Aug 19, 2013, 6:59 PM EDT
hi,
Please refer below link it might help to understand abstract class.
http://msdn.microsoft.com/en-us/library/k535acbf(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/vstudio/ms229047(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/aa645615(v=vs.71).aspx