Abstract Method From an Abstract Class Constructor

Question: Can you call an abstract method from an abstract class constructor?

Answer: Yes.

Let's look at an example.

Step 1

First we will create an console application name as InterviewQuestionPart8.

console application

Step 2

Now we will create an abstract class let's call it Customer and then we will create a abstract method call it Print(), let's now include a constructor and invoke Print() method within constructor, by following code.

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace InterviewQuestionPart8    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.     
  12.         }    
  13.         public abstract class Customer    
  14.         {    
  15.             public Customer()    
  16.             {    
  17.                 Print();    
  18.             }    
  19.             public abstract void Print();    
  20.         }    
  21.     }    
  22. }   
Here we invoke this abstract method within constructor of abstract class, so Can you call an abstract method from an abstract class constructor, the answer is yes.

Now the abstract method within the abstract class does not have implementation So what is the use of calling abstract method from the constructor of a abstract class to answer this question.

Step 3
 
Let's create the another class call it Savings Customer and make it inherite from abstract Customer class, now provide the implementation for the abstract print method.

Let's create one more class call it CorporateCustomer and similarly make it inherit from abstract Customer class and provide implementation for the abstract print method.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace InterviewQuestionPart8    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.     
  12.         }    
  13.         public abstract class Customer    
  14.         {    
  15.             public Customer()    
  16.             {    
  17.                 Print();    
  18.             }    
  19.             public abstract void Print();    
  20.         }    
  21.         public class SavingsCustomer : Customer    
  22.         {    
  23.             public override void Print()    
  24.             {    
  25.                 Console.WriteLine("SavingsCustomer class Print method invoked");    
  26.                    
  27.             }    
  28.         }    
  29.     
  30.         public class CorporateCustomer : Customer    
  31.         {    
  32.             public override void Print()    
  33.             {    
  34.                 Console.WriteLine("CorporateCustomer class Print method invoked");    
  35.     
  36.             }    
  37.         }    
  38.     }    
  39. }   
Now let's create instances of the derived classes within the Main method, by the following code.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Text;    
  4.     
  5. namespace InterviewQuestionPart8    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.             SavingsCustomer sc = new SavingsCustomer();    
  12.             CorporateCustomer cc = new CorporateCustomer();    
  13.     
  14.         }    
  15.         public abstract class Customer    
  16.         {    
  17.             public Customer()    
  18.             {    
  19.                 Print();    
  20.             }    
  21.             public abstract void Print();    
  22.         }    
  23.         public class SavingsCustomer : Customer    
  24.         {    
  25.             public override void Print()    
  26.             {    
  27.                 Console.WriteLine("SavingsCustomer class Print method invoked");    
  28.                    
  29.             }    
  30.         }    
  31.     
  32.         public class CorporateCustomer : Customer    
  33.         {    
  34.             public override void Print()    
  35.             {    
  36.                 Console.WriteLine("CorporateCustomer class Print method invoked");    
  37.     
  38.             }    
  39.         }    
  40.     }    
  41. }    
Step 4
 
Now run and see the output.

output

Now we are invoking the Print() method in Main() explicitly, so how is that being called?

Code description

So look at these classes, the SavingsCustomer class and the abstract Customer class; they are related to each other by inheritance. We now understand that when we create an instance of the derived class the parent class constructor is automatically called and for the Savings class the parent class is the Customer class and within the constructor of that class we invoked the Print() method so this overriden Print() method will be call automatically.

abstract class

abstract method

 


Similar Articles