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#.
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.
- 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.
- abstract public class Shape
- {
- }
The following code example declares an abstract base class, Shape, with an abstract method and an abstract property.
-
- abstract public class Shape
- {
-
- public abstract int Height { set; }
-
-
- public abstract int Area();
- }
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.
-
- public class Rectangle : Shape
- {
- private int width;
- private int height;
-
- public Rectangle(int height)
- {
- this.height = height;
- }
-
- public override int Height { set => height = value; }
- public int Width {set => width = value; }
-
- public override int Area()
- {
- return this.height * this.width;
- }
- }
-
-
- public class Square : Shape
- {
- private int width = 0;
- private int height;
-
- public Square(int w)
- {
- this.width = w;
- }
-
- public override int Height { set => height = value; }
-
- public override int Area()
- {
- return this.width * this.width;
- }
- }
Complete Code Example
The following code example is the complete console application written in .NET Core.
- using System;
-
- namespace Abstractsample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Square sq = new Square(10);
- Console.WriteLine($"Square area: {sq.Area()}");
-
- Rectangle rect = new Rectangle(50);
- rect.Width = 20;
- Console.WriteLine($"Rectangle area: {rect.Area()}");
-
- Console.ReadKey();
- }
- }
-
- public class Rectangle : Shape
- {
- private int width;
- private int height;
-
- public Rectangle(int height)
- {
- this.height = height;
- }
-
- public override int Height { set => height = value; }
- public int Width {set => width = value; }
-
- public override int Area()
- {
- return this.height * this.width;
- }
- }
-
-
- public class Square : Shape
- {
- private int width = 0;
- private int height;
-
- public Square(int w)
- {
- this.width = w;
- }
-
- public override int Height { set => height = value; }
-
- public override int Area()
- {
- return this.width * this.width;
- }
- }
-
-
- abstract public class Shape
- {
-
- public abstract int Height { set; }
-
-
- public abstract int Area();
- }
- }
Summary
In this article, I discussed what an abstract class is and how to implement an abstract class in C#.