How To Use An Abstract Class In C#

What is an abstract class in C#? This is one of the most frequently asked questions in .NET technical interviews. In this article, we will learn what an abstract class it, how to declare it, and how to use an abstract class in C#.

use an abstract class in csharp

What is an abstract class?

An abstract class in C# provides a blueprint of what its derived classes must implement before they can use it. An abstract class contains abstract members including abstract methods, abstract properties, abstract indexers, and abstract events. All abstract members of a class must be implemented by its derived classes.

Here are the key characteristics of abstract classes,

How to declare abstract methods in C#

An abstract method is declared by using the abstract modifier in a method declaration to indicate that the method is an abstract method and does not contain implementation. An abstract method is implicitly a virtual method.

Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.

  1. public abstract void AMethod();

The implementation is provided by a method override, which is a member of a non-abstract class.

How to declare abstract properties in C#

An abstract property is declared by using the abstract modifier in a property declaration to indicate that the property is an abstract method and does not contain implementation.

How to declare an abstract class in C#?

The abstract keyword is used to create an abstract class. The class definition starts with the abstract keyword.

The following code example declares an abstract base class, Shape.

  1. abstract public class Shape
  2. {
  3. }

The following code example declares an abstract base class, Shape, with an abstract method and an abstract property.

  1. // Abstract class
  2. abstract public class Shape
  3. {
  4. // Abstract properties
  5. public abstract int Height { set; }
  6. // Abstract method
  7. public abstract int Area();
  8. }

How to implement an abstract class in C#?

The following code implements the Shape abstract class by inheriting two classes, Rectangle and Square, from the class. Both derived classes must implement all abstract members. In our case, both classes implement Height property and Area method.

  1. // Rectangle class
  2. public class Rectangle : Shape
  3. {
  4. private int width;
  5. private int height;
  6. public Rectangle(int height)
  7. {
  8. this.height = height;
  9. }
  10. public override int Height { set => height = value; }
  11. public int Width {set => width = value; }
  12. public override int Area()
  13. {
  14. return this.height * this.width;
  15. }
  16. }
  17. // Square class
  18. public class Square : Shape
  19. {
  20. private int width = 0;
  21. private int height;
  22. public Square(int w)
  23. {
  24. this.width = w;
  25. }
  26. public override int Height { set => height = value; }
  27. public override int Area()
  28. {
  29. return this.width * this.width;
  30. }
  31. }

Complete Code Example

The following code example is the complete console application written in .NET Core.

  1. using System;
  2. namespace Abstractsample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. Square sq = new Square(10);
  9. Console.WriteLine($"Square area: {sq.Area()}");
  10. Rectangle rect = new Rectangle(50);
  11. rect.Width = 20;
  12. Console.WriteLine($"Rectangle area: {rect.Area()}");
  13. Console.ReadKey();
  14. }
  15. }
  16. // Rectangle class
  17. public class Rectangle : Shape
  18. {
  19. private int width;
  20. private int height;
  21. public Rectangle(int height)
  22. {
  23. this.height = height;
  24. }
  25. public override int Height { set => height = value; }
  26. public int Width {set => width = value; }
  27. public override int Area()
  28. {
  29. return this.height * this.width;
  30. }
  31. }
  32. // Square class
  33. public class Square : Shape
  34. {
  35. private int width = 0;
  36. private int height;
  37. public Square(int w)
  38. {
  39. this.width = w;
  40. }
  41. public override int Height { set => height = value; }
  42. public override int Area()
  43. {
  44. return this.width * this.width;
  45. }
  46. }
  47. // Abstract class
  48. abstract public class Shape
  49. {
  50. // Abstract properties
  51. public abstract int Height { set; }
  52. // Abstract method
  53. public abstract int Area();
  54. }
  55. }

Summary

In this article, I discussed what an abstract class is and how to implement an abstract class in C#.