Why Does An Abstract Class Needs A Constructor?

Introduction

 
Dear all, welcome back to a new article on a very interesting topic.
 
We always have wondered why we even need an abstract class in the first place? You will find the answer to that question in my recent article on Purposes Of An Abstract Class.
 
After understanding the need for an abstract class, it immediately raises a second question.
 
If it is a class, it can have a constructor to initialize its properties. But hold on, we know that abstract class can never be instantiated. which means we can never have an object of an abstract class. Then how are we supposed to call a constructor when we can't even create an object of an abstract class.
 
Hmmm, very interesting.
  1. public abstract class AppleBase  
  2.    {  
  3.        public AppleBase()  
  4.        {  
  5.   
  6.        }  
  7.        public abstract void SetPrice();  
  8.    } 
First, if I try to create an object of an abstract class what will happen?
 
 
There, it's a compile-time error. forget about running your program, not even complier is ready to allow that.
 
So basically we can not create an instance to call a constructor. But it is not the only way to call a constructor.
 
Ever heard about Constructor chaining
  •  Constructor Chaining is a concept when a constructor calls another constructor in the same class or its base class.
Let me explain it to you with an example.
 
Say we have base class AppleBase which is printing a message in its constructor.
  1. public class AppleBase  
  2.    {  
  3.        public AppleBase()  
  4.        {
  5.            Console.WriteLine("1. Base class: AppleBase");  
  6.        }  
  7.    } 
The derived class: MacBook. It is also printing a message in its constructor.
  1. class MacBook : AppleBase  
  2.    {  
  3.        public MacBook()  
  4.        {  
  5.            Console.WriteLine("2. Derived class: MacBook");  
  6.        }  
  7.    } 
Now if I create an object of MacBook. what do you think what will happen?
  1. class Program  
  2.    {  
  3.        static void Main(string[] args)  
  4.        {  
  5.            MacBook macBook = new MacBook();  
  6.        }  
  7.    } 
Let's see what will happen.
 
 
Analyze the output, even if we create an object of a MacBook, Compiler still calls an AppleBase class's constructor prior to MacBook's.
 
This concept is known as constructor chaining. 
 
What did this tell us?
 
Exactly my thoughts. which means we do have another way to call a constructor.
 
Object creation does not hold a monopoly over constructor executions, Right?
 
So what do we know?
  • We know that Abstract classes can have constructors.
  • And we can not instantiate an abstract class.
What did we learn?
  • We can call the Abstract class's constructor through constructor chaining.
 Let's have our abstract class back and let's have an abstract method to define the name of the product.
  1. public abstract class AppleBase  
  2.    {  
  3.        public AppleBase()  
  4.        {  
  5.        }  
  6.   
  7.        public abstract void SetName();  
  8.    } 
Derived class MacBook, Overriding a SetName method.
  1. class MacBook : AppleBase  
  2.    {  
  3.        public MacBook()  
  4.        {  
  5.             
  6.        }  
  7.        public override void SetName()  
  8.        {  
  9.            Console.WriteLine("Name: MacBook Pro");  
  10.        }  
  11.    } 
Let us execute this in the Main method.
  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           MacBook macBook = new MacBook();  
  6.           macBook.SetName();  
  7.       }  
  8.   } 
Let's see an output.
 
 
It works, but to take advantage of that constructor in an abstract class, we have to automate something.
 
It could be initializing properties or anything.
 
In our case, we can directly call an abstract method SetName() inside a constructor. So that we wouldn't need of calling SetName() method with an object of MacBook in the Main method.
  1. public abstract class AppleBase    
  2.    {    
  3.        public AppleBase()    
  4.        {    
  5.            SetName();    
  6.        }    
  7.     
  8.        public abstract void SetName();    
  9.    }    
  10.     
  11. class Program    
  12.    {    
  13.        static void Main(string[] args)    
  14.        {    
  15.            MacBook macBook = new MacBook();       
  16.           //macBook.SetName();  
  17.        }    
  18.    }   
And the output will be the same.
 
Amazing. Finally, mystery solved. Pheww.
 

Conclusion

 
In this article, we learned.
  • What is the purpose of the constructor inside an abstract class?
  • What is constructor chaining?
  • How to utilize constructors of abstract classes.
With that being said, I wish you all the very best.
 
Code with clear knowledge. Basic has to be strong to become efficient in coding.
 
And as always, Happy Coding!
 
Wish to connect? Find me @


Similar Articles