1. Introduction
Pre-requisites
2. Roadmap
- Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
- Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
- Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
- Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
- Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
- Diving in 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)
- Diving into OOP (Day 9): Understanding Events in C# (An Insight)
3. Abstract Classes
“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
InheritanceAndPolymorphism
” in your Visual Studio. You’ll get a class namedProgram.cs, just add one more class named ClassA.cs, note that the ClassA
should be marked abstract
, and the following code to ClassA.cs and Program.cs: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();
}
}
}
Output
Compile time error: Cannot create an instance of the abstract class or interface 'InheritanceAndPolymorphism.ClassA'
abstract
class using new
keyword.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 new
keyword. Seems that the class is useless for us as we cannot use it for other practical purposes as we used to do.5. Non Abstract Method Definition in Abstract Class
abstract
class:/// <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();
}
}
new
if we have already used an abstract
modifier.6. Abstract Class Acting as a Base Class
/// <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();
}
}
abstract
class. Creating an object of ClassB
does not gives us any error.abstract
class.abstract
class can create an object.7. Non Abstract Method Declaration in Abstract Class
/// <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();
}
}
YYY()
in our abstract
class ClassA
.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 with the logic.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
YYY()
as abstract
in ClassA
:/// <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()'
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.YYY()
as abstract
in ClassA
. Since ClassB
derives fromClassA
, 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
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();
}
}
Output
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()’.
override
keyword. Otherwise add the new
keyword.YYY()
.override
keyword before derived class method YYY()
./// <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 override 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();
}
}
10. Abstract Method Implementation in Derived Class with Different Return Type
YYY()
in derived class:/// <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 override int 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();
}
}
YYY
from void
to int
in derived class. Compile the code.Output
Compile time error: 'InheritanceAndPolymorphism.ClassB.YYY()': return type must be 'void' to match overridden member 'InheritanceAndPolymorphism.ClassA.YYY()'
abstract
method from a derived class, we cannot change the parameters passed to it or the return type irrespective of the number of methods declared as abstract
in abstract
class./// <summary> /// Abstract class ClassA /// </summary> public abstract class ClassA { public int a; public void XXX() {
}
abstract public void YYY();
abstract public void YYY1();
abstract public void YYY2();
abstract public void YYY3();
}
/// <summary>
/// Derived class.
/// Class derived from abstract class ClassA.
/// </summary>
public class ClassB:ClassA
{
public override int 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();
}
}
'InheritanceAndPolymorphism.ClassB' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassA.YYY3()'
‘InheritanceAndPolymorphism.ClassB’ does not implement inherited
abstract member ‘InheritanceAndPolymorphism.ClassA.YYY2()’
‘InheritanceAndPolymorphism.ClassB’ does not implement inherited
abstract member ‘InheritanceAndPolymorphism.ClassA.YYY1()’
abstract
class means that the class is incomplete and cannot be directly used. Anabstract
class can only be used as a base class for other classes to derive from.11. Variable Initialization in Abstract Class
new
keyword on an abstract
class. If we do not initialize a variable in an abstract
class like we used a
, it will automatically have a default value of 0
which is what the compiler kept warning us about. We can initialize int
variable a
of ClassA
to any value we wish. The variables in abstract
class act similar to that in any other normal class.12. Power of Abstract Class
abstract
and the class is marked abstract
as well. And so, we can compile our class without any error or blocker. Any other class can then derive from our abstract
class but they have to implement the abstract
, i.e., our incomplete methods from abstract
class.Abstract
therefore enables us to write code for a part of the class and allows the others (derived classes) to complete the rest of the code.13. Abstract Method in Non Abstract Class
/// <summary> /// Abstract class ClassA /// </summary> public 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 override 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();
}
}
Output
Compiler error: 'InheritanceAndPolymorphism.ClassA.YYY()' is abstract but it is contained in non-abstract class 'InheritanceAndPolymorphism.ClassA'
abstract
keyword from class ClassA
. The error clearly conveys a message that if a single method is marked abstract
in a class, then the class will have to be abstract
as well.abstract
method, then the class has to be declared abstract
as well.abstract
method also cannot use the modifiers such as static
or virtual
.abstract
method in an abstract
class. Any class that derives from abstract
class has to give implementation to its abstract
method. By default, the modifier new
gets added to the derived class method, that makes it a new/different method.14. Abstract Base Method
/// <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 override void YYY()
{
base.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();
}
}
Output
Compile time error : Cannot call an abstract base member: 'InheritanceAndPolymorphism.ClassA.YYY()'
YYY()
from the base class ClassA
as it does not carry any implementation/code along with it and has also been declared abstract
. Common sense prevails? and C# off course does not allow us to call a method that does not contain code.15. Abstract Class Acting as Derived as Well as Base Class
/// <summary> /// Base class ClassA /// </summary> public class ClassA { public virtual void XXX() { Console.WriteLine("ClassA XXX"); } }
/// <summary>
/// Derived abstract class.
/// Class derived from base class ClassA.
/// </summary>
public abstract class ClassB:ClassA
{
public new abstract void XXX();
}
public class ClassC:ClassB
{
public override void XXX()
{
System.Console.WriteLine(“ClassC XXX”);
}
}
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
ClassA classA = new ClassC();
ClassB classB = new ClassC();
classA.XXX(); classB.XXX();
}
}
Output
ClassA XXX ClassC XXX
ClassA
that is not abstract
and added a virtual method XXX
to it. Since the method is non abstract
but marked virtual
so it has to be overridden in its deriving class. We added one more class named ClassB
and marked that class abstract
, note that this class is derived from ClassA
. So this class has a choice to override the method marked as virtual in base class. But we’ll do something different and tricky,XXX
method in this derived class as new abstract
, and did not give anybody to this method. Now what? We will add one more class ClassC
, that will derive from ClassB
. ClassC
has no choice but to override the methodXXX
. Therefore we override the method XXX
in ClassC
.ClassA classA = new ClassC();
and ClassB classB = new ClassC();
ClassC
but refers to ClassA
and second one again seems to be like ClassC
but refers to ClassB
.classA.XXX()
will definitely first look into the class ClassA
. Here, it finds the method XXX
marked as virtual. These kind of scenarios we have already taken n number of times in our earlier articles where we discussed about run time polymorphism . C# will then crawl over to class ClassB
. Here it gets shocked that the method XXX()
is abstract
, i.e., there is no code or implementation for method XXX()
and also that it is a method marked as new
, thus severing all links with the base class. And so flow halts and all and the method XXX()
from ClassA
gets executed.b.XXX()()
, since the method is new
, the links to the base class gets broken, we are left with no choice but to invoke the method from ClassC
as it says override
.new
with the keyword override
for the method XXX()
in abstract
class ClassB
.override
modifier in ClassC
with “new
” like:public class ClassC:ClassB { public new void XXX() { System.Console.WriteLine("ClassC XXX"); } }
Output
Compile time error: 'InheritanceAndPolymorphism.ClassC' does not implement inherited abstract member 'InheritanceAndPolymorphism.ClassB.XXX()'
XXX
. Remember the XXX()
of class ClassA
has nothing to do at all with that of ClassB
and ClassC
.16. Can Abstract Class be Sealed?
/// <summary> /// sealed abstract class ClassA /// </summary> public sealed abstract class ClassA { public abstract void XXX() { Console.WriteLine("ClassA XXX"); } }
/// <summary>
/// Program: used to execute the method.
/// Contains Main method.
/// </summary>
public class Program
{
private static void Main(string[] args)
{
}
}
Output
Compile time error: 'InheritanceAndPolymorphism.ClassA': an abstract class cannot be sealed or static
Abstract
class cannot be sealed
class.Abstract
class cannot be a static
class.17. Points to Remember
- We cannot create an object of
abstract
class usingnew
keyword. - A class can be derived from an
abstract
class. - Class derived from an
abstract
class can create an object. - If we declare any method as
abstract
in ourabstract
class, then it’s the responsibility of the derived class to provide the body of thatabstract
method, unless a body is provided for thatabstract
method, we cannot create an object of that derived class. - When we override an
abstract
method from a derived class, we cannot change the parameters passed to it or the return type irrespective of the number of methods declared asabstract
inabstract
class. - An
abstract
class means that the class is incomplete and cannot be directly used. Anabstract
class can only be used as a base class for other classes to derive from. - If a class has even a single
abstract
method, then the class has to be declaredabstract
as well. - An
abstract
method also cannot use the modifiers such asstatic
orvirtual
. Virtual
methods run slower that non virtual methods.Abstract
class cannot besealed
class.Abstract
class cannot be astatic
class.
18. Conclusion
Abstract
classes are one of my favorites so I just wanted to take them separately. I hope my readers enjoyed this article too and learnt about abstract
classes in C#.Read more:
- C# and ASP.NET Questions (All in one)
- MVC Interview Questions
- C# and ASP.NET Interview Questions and Answers
- Web Services and Windows Services Interview Questions
Other Series
My other series of articles:
For more informative articles visit my Blog.
7 comments