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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
VulpesPosted Aug 19, 2013, 7:44 AM
The output is:
Jignesh TrivediPosted Aug 19, 2013, 10:59 PM
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
VijayPosted Aug 19, 2013, 7:19 AM
VulpesPosted Aug 19, 2013, 6:57 AM
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.
VijayPosted Aug 19, 2013, 6:29 AM
we can create like this?
VulpesPosted Aug 19, 2013, 5:33 AM
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.