Part 9 Call an abstract method from an abstract class constructor04:33

  • 0
Published on December 2, 2017

Link for code samples used in the demo

Link for csharp, asp.net, ado.net, dotnet basics, mvc and sql server video tutorial playlists

Can you call an abstract method from an abstract class constructor? If so, what is the use of it?
Yes, an abstract method can be called from an abstract class constructor. Here is an example.
public class Program
{
public static void Main()
{
CorporateCustomer CC = new CorporateCustomer();
SavingsCustomer SC = new SavingsCustomer();
}
}

public abstract class Customer
{
protected Customer()
{
Print();
}

public abstract void Print();
}

public class CorporateCustomer : Customer
{
public override void Print()
{
Console.WriteLine(“CorporateCustomer Print() method implementation called”);
}
}

public class SavingsCustomer : Customer
{
public override void Print()
{
Console.WriteLine(“SavingsCustomer Print() method implementation called”);
}
}

An abstract method in an abstract class does not have any implementation, so what is the use of calling it from the abstract class constructor?
If you want the abstract method to be invoked automatically whenever an instance of the class that is derived from the abstract class is created, then we would call it in the constructor of the abstract class.

Enjoyed this video?
"No Thanks. Please Close This Box!"