Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at detailed Introduction to Abstract Classes in C#. It's our 19th tutorial in C# series. Today, we are gonna discuss another new concept in C# and I would recommend you to follow these tutorials at least 3 to 4 times, so that these concepts find the right place in your brain and stick there.C# Abstract Classes are quite similar to C# Interface in many respects, so if you have studied previous tutorial in C# Series, then do read it first. So, let's get started with Introduction to Abstract Classes in C#:
Introduction to Abstract Classes in C#
- Abstract Classes in C# are created by using Abstract Keyword, and are used to create abstract members, which have their declaration but no implementation (as in C# Interfaces) but unlike Interfaces, Abstract Classes can also have complete methods, properties etc.
- Abstract Methods in C# are also created by using Abstract Keyword, these methods are only declared in Abstract classes and are then implemented in one of its derived classes & the implementation of the Method must have override Keyword in it.
- You must be finding these definitions a bit difficult but no need to worry as we will discuss them one by one in detail.
- The Abstract Modifier / Keyword is used to indicate an incomplete implementation that's why we can't instantiate such objects. We can use it with classes, methods, properties, events and Indexers.
- Moreover, the class inherited from Abstract Class must have the complete implementation of all the members of Abstract Class, along with override keyword in their declaration. (We will discuss it shortly)
- It's not compulsory for the Abstract Class to must have some Abstract members, instead we can add only normal members as well.
- Let's have a look at the syntax of Abstract Class having 1 Abstract Method & 1 normal Method, shown in below figure:
- In the above figure, we have created a public class with abstract keyword/modifier in it named StudentsAbstract, so it's an abstract class.
- As StudentsAbstract is an Abstract Class, so we can create both Abstract (Incomplete) members and normal (complete) members in it.
- So, I have created two methods, printMsg() is an abstract Method as it has Abstract Keyword in its declaration, that's why we haven't placed the implementation of this method.
- While printMsg2() is a normal method so it's fully implemented in Abstract Class.
- These Abstract Classes are normally used as a Parent Class in Inheritance and it's kind of their main role. So, let's have a look at Inheritance in Abstract Classes:
Inheritance in C# Abstract Classes
- As I mentioned earlier, Inheritance is the main role of C# Abstract Classes and they normally play the role of Parent Class and we inherit different classes & structs from them.
- We can't use the sealed keyword with abstract class, as then we can't inherit any other class. ( We have studied sealed keyword in C# Classes Lecture )
- Abstract Class can inherit from other Abstract Classes & C# Interfaces. ( Interfaces can't inherit from Abstract Classes )
- The class inherited from Abstract Class, must have the implementation of all abstract members of its Parent Class, otherwise, we will get a compiler error.
- In the above figure, we can see that I have added a new class StudentsClass derived from our Abstract Class StudentsAbstract.
- Now, this new derived class must have the implementation of its Parent's Abstract Members, as I did for printMsg() Method in above figure.
- Moreover, we need to use override keyword in our derived class method's declaration.
- So, now we have created an Abstract Class and have also added an Abstract Method in it, after that we have derived a new class and provided implementation of our Abstract Method.
- Let's now invoke this Abstract Method, declared in Abstract Class & Implemented in Derived Class, shown in below figure:
- In the Main function, I have simply created a reference variable of Child Class and then invoked both of these methods.
- You must have noticed that Abstract Classes are quite similar to Interfaces but they have few differences as well, let's have a quick look at them:
Abstract Classes Vs. Interfaces
- Abstract Classes & Interfaces have a lot in common as both are used for creating incomplete members i.e. declaration only, yet they have some differences as well.
- We can't create implementation of any member in Interfaces but that's not the case in Abstract Classes, we have the option to create Full members in Abstract Classes.
- Members in Interfaces can't use access modifier and they are all public by default, but in Abstract Classes we can provide access modifier and can make them private etc.
- We can't declare C# Fields in Interfaces but that's possible in Abstract Classes.