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.
- public abstract class AppleBase
- {
- public AppleBase()
- {
-
- }
- public abstract void SetPrice();
- }
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.
- public class AppleBase
- {
- public AppleBase()
- {
- Console.WriteLine("1. Base class: AppleBase");
- }
- }
The derived class: MacBook. It is also printing a message in its constructor.
- class MacBook : AppleBase
- {
- public MacBook()
- {
- Console.WriteLine("2. Derived class: MacBook");
- }
- }
Now if I create an object of MacBook. what do you think what will happen?
- class Program
- {
- static void Main(string[] args)
- {
- MacBook macBook = new MacBook();
- }
- }
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.
- public abstract class AppleBase
- {
- public AppleBase()
- {
- }
-
- public abstract void SetName();
- }
Derived class MacBook, Overriding a SetName method.
- class MacBook : AppleBase
- {
- public MacBook()
- {
-
- }
- public override void SetName()
- {
- Console.WriteLine("Name: MacBook Pro");
- }
- }
Let us execute this in the Main method.
- class Program
- {
- static void Main(string[] args)
- {
- MacBook macBook = new MacBook();
- macBook.SetName();
- }
- }
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.
- public abstract class AppleBase
- {
- public AppleBase()
- {
- SetName();
- }
-
- public abstract void SetName();
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- MacBook macBook = new MacBook();
-
- }
- }
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 @