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.
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.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace InterviewQuestionPart8
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- }
- public abstract class Customer
- {
- public Customer()
- {
- Print();
- }
- public abstract void Print();
- }
- }
- }
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.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace InterviewQuestionPart8
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- }
- public abstract class Customer
- {
- public Customer()
- {
- Print();
- }
- public abstract void Print();
- }
- public class SavingsCustomer : Customer
- {
- public override void Print()
- {
- Console.WriteLine("SavingsCustomer class Print method invoked");
-
- }
- }
-
- public class CorporateCustomer : Customer
- {
- public override void Print()
- {
- Console.WriteLine("CorporateCustomer class Print method invoked");
-
- }
- }
- }
- }
Now let's create instances of the derived classes within the Main method, by the following code.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace InterviewQuestionPart8
- {
- class Program
- {
- static void Main(string[] args)
- {
- SavingsCustomer sc = new SavingsCustomer();
- CorporateCustomer cc = new CorporateCustomer();
-
- }
- public abstract class Customer
- {
- public Customer()
- {
- Print();
- }
- public abstract void Print();
- }
- public class SavingsCustomer : Customer
- {
- public override void Print()
- {
- Console.WriteLine("SavingsCustomer class Print method invoked");
-
- }
- }
-
- public class CorporateCustomer : Customer
- {
- public override void Print()
- {
- Console.WriteLine("CorporateCustomer class Print method invoked");
-
- }
- }
- }
- }
Step 4
Now run and see the output.
Now we are invoking the Print() method in Main() explicitly, so how is that being called?
Code descriptionSo 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.