An abstract class is an incomplete class because it does not have an implementation for all of its members since they are abstract and an abstract class cannot be sealed.
We all know how to create a class.
To create a class we do the following:
- public class NonAbstractClass {
-
- }
The preceding class is a non-abstract class and to
create an
abstract class, we use an
abstract keyword before the class keyword.
- public abstract class MyAbstractClass {
-
- }
Within a class, any member can become an abstract member, for example, properties, events, methods and so on. And how can we make the members abstract?
Using an abstract keywordLet's look at an example where I want a method that is an abstract method.
- public abstract class MyAbstractClass {
- public void MyAbstractMethod() {
-
- }
- }
Currently the preceding,
MyAbstractMethod is a non-abstract method and to make it abstract, all we need to do is use an abstract keyword.
- public abstract class MyAbstractClass {
- public abstract void MyAbstractMethod() {
-
- }
- }
But when we build our solution, we get a compile time error stating we cannot declare a body because it is marked abstract.
The reason is, we can only provide an implementation for the abstract members in a class that inherits the abstract class.
- public abstract class MyAbstractClass {
- public abstract void MyAbstractMethod();
- }
So now, we have an abstract class with an abstract member.
Instance of an abstract class
I told you earlier that abstract classes are incomplete because they do not have an implementation for all of its members since they are abstract and since they are abstract, it doesn't make any sense to be able to create an instance of this class and use those members to do something. That's why
you cannot create an instance of an abstract class and they
can only be used as a base class for other classes.
In my project, I have another class named “
Program” that is a non-abstract class. Now what we will do is, we will inherit our Program class from the abstract class that we have created, in other words
MyAbstractClass.
If you look at this program class, it is a non-abstract class because we haven't used an abstract keyword.
- public class Program {
- static void Main(string[] args) {
- }
- }
When a non-abstract class inherits from an abstract class, then a non-abstract class must provide an implementation for all the abstract members of an abstract/base class.
- public class Program : MyAbstractClass{
- static void Main(string[] args) {
- }
- }
If we try to compile our project we will now get a compile-time error stating that the Program class does not implement an inherited abstract member that is present in the abstract/base class.
If your program class doesn't wish to provide an implementation, then you can mark your program class as an abstract class.
- public abstract class Program : MyAbstractClass{
- static void Main(string[] args) {
- }
- }
Now if you build the solution, you won't get an error.
But on the other hand, if you don't want your Program class to be marked as abstract then provide an implementation for all the abstract members and provide an implementation for the abstract members. Use an override keyword for that.
- class Program : MyAbstractClass{
- override space abstractMethod press enter
- class Program : MyAbstractClass{
- public override void MyAbstractMethod() {
- throw new NotImplementedException();
- }
- }
Remove the default implementation and provide your own.
- class Program : MyAbstractClass {
- public override void MyAbstractMethod() {
- Console.WriteLine("my abstract method implementation");
- }
- }
To print the implementation on the console screen, we need to create an instance of our Program class that is a non-abstract class.
- class Program : MyAbstractClass {
- public override void MyAbstractMethod() {
- Console.WriteLine("my abstract method implementation");
- }
- static void Main(string[] args) {
- Program p = new Program();
- p.MyAbstractMethod();
- }
- }
You can do the same by using a base reference variable pointing to your child class object.
- static void Main(string[] args) {
- MyAbstractClass myAbstract = new Program();
- myAbstract.MyAbstractMethod();
- }
The output will be the same.
An abstract class cannot be sealed. We have learned, if you want to
prevent your
class from being
inherited by another class then we use a
sealed keyword. But if we go by the definition, an abstract class can only be used as a base class and so it cannot be sealed.
- abstract sealed class MyAbstractClass {
-
- }
Build your program.
An abstract class may contain abstract members but not mandatory.Here I have an abstract class
MyAbstractClass with an abstract member
MyAbstractMethod but I can also have a member in the same abstract class
MyNonAbstractMethod that is a non-abstract member.
- abstract class MyAbstractClass {
- public abstract void MyAbstractMethod();
- public void MyNonAbstractMethod() {
- Console.WriteLine("my non-abstract method implementation");
- }
- }
If we build our program, we will not get an error.
Important Note
If a class inherits an abstract class, provide an implementation for all the abstract members inherited from the base abstract class.
If a class does not wish to provide an implementation for all the abstract members inherited from the abstract base class, then the class must be marked as abstract.