1. Introduction
We have learned a lot about polymorphism and Inheritance. In this article of the series “Diving Into OOP”, we'll discuss the hottest and most exciting topic of OOP in C#, Abstract Classes. The concept of Abstract classes is the same for any other language, but in C# we deal with it in a bit of a different way. Abstract classes play a different and very interesting role in polymorphism and inheritance. We'll cover all the aspects of abstract classes with our hands-on lab and theory as an explanation to what output we get. We'll also list the points to remember at the end of the article.

Pre-requisites
I wonder, we are dealing with the fourth part of our learning objective. Now my only expectation with my readers is to enjoy the series.
2. Roadmap
Let's recall our road map.
- Diving Into OOP (Day 1) : Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism).
- Diving into OOP (Day 2) : Polymorphism and Inheritance (Inheritance).
- Diving into OOP (Day 3) : Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism).
- Diving into OOP (Day 4) : Polymorphism and Inheritance (Abstract Classes).
- Diving into OOP (Day 5): All about access modifiers in C#(Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields).
- Diving into OOP (Day 6) : Understanding Enum in C# (A Practical Approach).
- Diving into OOP (Day 7): Properties in C# (A Practical Approach).
- Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
- Learning C# (Day 9): Delegates in C# (A Practical Approach)
- Learning C# (Day 10): Events in C# (A Practical Approach)
The following definition is from the MSDN:
"The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.
Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method.”
4. Abstract classes in action
Add a console application named “InheritanceAndPolymorphism” in your Visual Studio. You'll get a class named Program, just add one more class named ClassA. Note that ClassA should be marked abstract and add the following code to ClassA and the Program class:
- using System;
- namespace InheritanceAndPolymorphism
- {
- public abstract class ClassA
- {
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassA classA = new ClassA();
- Console.ReadKey();
- }
- }
- }
Compile the code.
Output
Compile time error: Cannot create an instance of the abstract class or interface 'InheritanceAndPolymorphism.ClassA'
Point to remember: We cannot create an object of abstract class using the new keyword.
Now we go into understanding the concept. No power can stop abstract keyword to be written before a class. It acts as a modifier to the class. We cannot create an object of abstract class using the new keyword. It seems that the class is useless for us since we cannot use it for other practical purposes as we previously did.
5. Non abstract method definition in abstract class
Let's add some code to our abstract class as in the following:
- /// <summary>
- /// Abstract class ClassA
- /// </summary>
- public abstract class ClassA
- {
- public int a;
- public void XXX()
- {
- }
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassA classA = new ClassA();
- Console.ReadKey();
- }
- }
We again see the error that we encountered earlier. Again it reminds us that we cannot use new if we have already used an abstract modifier.
6. Abstract class acting as a base class
Let's add one more class as in the following:
- /// <summary>
- /// Abstract class ClassA
- /// </summary>
- public abstract class ClassA
- {
- public int a;
- public void XXX()
- {
- }
- }
- /// <summary>
- /// Derived class.
- /// Class derived from abstract class ClassA
- /// </summary>
- public class ClassB:ClassA
- {
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassB classB = new ClassB();
- Console.ReadKey();
- }
- }
We get no error. A class can be derived from an abstract class. Creating an object of ClassB does not give us an error.
Point to remember: A class can be derived from an abstract class.
Point to remember: A class derived from an abstract class can create an object.
7. Non abstract method declaration in abstract class
Another scenario:
- /// <summary>
- /// Abstract class ClassA
- /// </summary>
- public abstract class ClassA
- {
- public int a;
- public void XXX()
- {
- }
- public void YYY();
} - /// <summary>
- /// Derived class.
- /// Class derived from abstract class ClassA.
- /// </summary>
- public class ClassB:ClassA
- {
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassB classB = new ClassB();
- Console.ReadKey();
- }
- }
We just declared a method named YYY() in our abstract class ClassA.
Compile the code; we get:
Output
Compile time error: 'InheritanceAndPolymorphism.ClassA.YYY()' must declare a body because it is not marked abstract, extern, or partial
InheritanceAndPolymorphism is the namespace I used for my console application so you can ignore that, no need to confuse it with the logic.
In the code above, we just added a method declaration in the abstract class. An abstract method indicates that the actual definition or code of the method is created somewhere else. The method prototype declared in abstract class must also be declared abstract as per the rules of C#.
8. Abstract method declaration in abstract class
Just make the method YYY() as abstract in ClassA as in the following:
- /// <summary>
- /// Abstract class ClassA
- /// </summary>
- public abstract class ClassA
- {
- public int a;
- public void XXX()
- {
- }
- abstract public void YYY();
- }
- /// <summary>
- /// Derived class.
- /// Class derived from abstract class ClassA.
- /// </summary>
- public class ClassB:ClassA
- {
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassB classB = new ClassB();
- Console.ReadKey();
- }
- }
Output
Compiler error: 'InheritanceAndPolymorphism.ClassB' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()'
Point to remember: If we declare any method as abstract in our abstract class, then it's the responsibility of the derived class to provide the body of that abstract method, unless a body is provided for that abstract method, we cannot create an object of that derived class.
In the previously specified scenario, we declared method YYY() as abstract in ClassA. Since ClassB derives from ClassA, now it becomes the responsibility of ClassB to provide the body of that abstract method, else we cannot create an object of ClassB.
9. Abstract method implementation in derived class
Now provide a body of method YYY() in ClassB, let's see what happens,
- /// <summary>
- /// Abstract class ClassA
- /// </summary>
- public abstract class ClassA
- {
- public int a;
- public void XXX()
- {
- }
- abstract public void YYY();
- }
- /// <summary>
- /// Derived class.
- /// Class derived from abstract class ClassA.
- /// </summary>
- public class ClassB:ClassA
- {
- public void YYY()
- {
- }
- }
- /// <summary>
- /// Program: used to execute the method.
- /// Contains Main method.
- /// </summary>
- public class Program
- {
- private static void Main(string[] args)
- {
- ClassB classB = new ClassB();
- Console.ReadKey();
- }
- }
Every thing seems fine now, but now compile the code.
Output
There are two compile-time errors this time.
Compile time error: 'InheritanceAndPolymorphism.ClassB' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY()'
Compile time warning: 'InheritanceAndPolymorphism.ClassB.YYY()' hides inherited member 'InheritanceAndPolymorphism.ClassA.YYY()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

We have been continuously trying to compile our code, but with no success until now. The compiler error indicates clearly that both of our base and derived classes contain the same method named YYY().
If both our derived class and base class contains the method with the same name, an error will always occur. The only way to overcome this error is to derive a class explicitly and add the modifier override to its method signature. We have already discussed such scenarios in our previous parts of the articles of Diving Into OOP series.
Let's add the override keyword before the derived class method YYY().





NicksPosted Jan 31, 2017, 5:20 AM
Very nice article. It refreshed my abstract class concept.
Vipin TyagiPosted Feb 18, 2016, 2:16 AM
I am reading this article series since last 3 days.It is just awesome.Concepts are more clear.Thank you so much.Akhil Mittal cc Mahesh Chand.#18 Feb 2016
Former memberPosted Jul 1, 2015, 2:59 AM
Great One
Gowtham RajamanickamPosted May 20, 2015, 1:16 AM
simply sooperb
Gowtham RajamanickamPosted May 20, 2015, 1:15 AM
Great article...
Abhishek KumarPosted Jul 9, 2014, 3:45 AM
Great article Akhil....Nicely Explained....
Manju lata YadavPosted Jul 9, 2014, 1:16 AM
Article is superb
JASPALPosted Jul 8, 2014, 8:07 AM
Under Point 10 "If we implement these three methods in a derived class then we'll get no error." But we still get an error because of 'ConsoleApplication1.ClassB.YYY()': return type must be 'void' to match overridden member 'ConsoleApplication1.ClassA.YYY()'
SRI PREMPosted Jul 8, 2014, 6:08 AM
Nice..Got a clear understanding about OOP Concepts. Q:IN line 15 : It looks like you are equating Object of Class B=Class C is that possible ? ( looks like we are initiating an object of Class B there)
Amar BishtPosted Jun 25, 2014, 2:48 AM
very Nice Article..
Chris EarglePosted Jun 23, 2014, 2:42 PM
Hello Akhil, when you said that an abstract class cannot be directly used, I think you mean it cannot be directly instantiated. In the article, you cast to the abstract class and use it without any problems.
Parveen AroraPosted Jun 5, 2014, 9:18 AM
Can you please expand the line "No power can stop abstract keyword to be written before a class." that is written in Point 4, Point to Remember section.
Pankaj BajajPosted Jun 4, 2014, 12:51 PM
Thanks Akhil for sharing such kind of articles..Your OOP's series article's are very great..
Gopi ChandPosted Jun 3, 2014, 9:57 AM
Such a nice article sir with wide information about the topic...and inspiring also..
ad codersPosted Jun 2, 2014, 11:24 AM
One stop for learning abstract classes..good
Anil KumarPosted Jun 2, 2014, 6:32 AM
Good one, be careful while taking images, they may be copyright protected.