Using Abstract Class In C#

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,

  • An abstract class cannot be instantiated.
  • A class may inherit one abstract class only.
  • An abstract class may contain abstract methods, properties, and events.
  • An abstract class can’t be modified with the sealed class modifier.
  • A derived from an abstract class must implement all inherited abstract methods and accessors

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.   
  7.     // Abstract method  
  8.     public abstract int Area();  
  9. }  

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.   
  7.     public Rectangle(int height)  
  8.     {  
  9.         this.height = height;  
  10.     }  
  11.   
  12.     public override int Height { set => height = value; }  
  13.     public int Width {set => width = value; }  
  14.   
  15.     public override int Area()  
  16.     {  
  17.         return this.height * this.width;  
  18.     }  
  19. }  
  20.   
  21. // Square class   
  22. public class Square : Shape  
  23. {  
  24.     private int width = 0;  
  25.     private int height;  
  26.   
  27.     public Square(int w)  
  28.     {  
  29.         this.width = w;  
  30.     }  
  31.   
  32.     public override int Height { set => height = value; }  
  33.   
  34.     public override int Area()  
  35.     {  
  36.         return this.width * this.width;  
  37.     }  
  38. }  

Complete Code Example 

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

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

Summary

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


Similar Articles