Part 8 Can an abstract class have a constructor

Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat/playlists

Can an abstract class have a constructor? If so what is the use?
Yes, an abstract class can have a constructor. In general, a class constructor is used to initialise fields. Along the same lines, an abstract class constructor is used to initialise fields of the abstract class. You would provide a constructor for an abstract class if you want to initialise certain fields of the abstract class before the instantiation of a child-class takes place. An abstract class constructor can also be used to execute code that is relevant for every childclass. This prevents duplicate code.

Before an instance of CorporateCustomer or SavingsCustomer class is created, a Globally Unique Identifier (GUID) must be stored in the _id field of the abstract class. This can be achieved using abstract class constructor as shown below. Since initialisation is done in the base abstract Customer class, we don’t have to duplicate this logic in CorporateCustomer and SavingsCustomer classes.
public class Program
{
public static void Main()
{
CorporateCustomer CC = new CorporateCustomer();
Console.WriteLine(CC.Id);

SavingsCustomer SC = new SavingsCustomer();
Console.WriteLine(SC.Id);
}
}

public abstract class Customer
{
public Customer()
{
this._id = Guid.NewGuid();
}

public Guid Id
{
get
{
return this._id;
}
}

private Guid _id;

// Other abstract members
// public abstract void Save();
}

public class CorporateCustomer : Customer
{
}

public class SavingsCustomer : Customer
{
}

You cannot create an instance of an abstract class. So, what is the use of a constructor in an abstract class?
Though you cannot create an instance of an abstract class, we can create instances of the classes that are derived from the abstract class. So, when an instance of derived class is created, the parent abstract class constructor is automatically called.

Note: Abstract classes can’t be directly instantiated. The abstract class constructor gets executed thru a derived class. So, it is a good practice to use protected access modifier with abstract class constructor. Using public doesn’t make sense.

    .Net abstract abstract class advantage Advantages array arraylist AS asp.net benefits block C (Programming Language) C# c# class default constructor access modifier c# interview questions and answers c# reflection constructor access modifier c# reflection constructor not found c# reflection default constructor c# reflection get constructor c# reflection private constructor c# reflection protected constructor call cast catch class constructor csharp Data DataTypes difference between Difference between int and Int32 in c# different directory dot net dotnet Each example exception exceptions factorial files Finally folder frequently asked function generic Handle happens int vs int32 interfaces interview is jagged keyword LINQ list Lists method Number occur Operator Overriding purpose question questions Real Real-time real-world realtime Recursion recursive reverse Sentence single store String Structure subdirectories Throw throws time try Types Use using virtual method what when where Why Word World