Abstract Class in C# with Example

โšก Smart Summary

C# Abstract Class defines a blueprint that other classes inherit but cannot instantiate directly. This resource explains abstraction, the abstract keyword, virtual methods, and a worked C# example of base and child classes.

  • ๐Ÿงฑ Core Concept: An abstract class is marked with the abstract keyword and cannot be instantiated on its own.
  • ๐Ÿงฌ Inheritance Role: It acts as a base class that subclasses inherit, then implement or override its methods.
  • ๐Ÿ”‘ Virtual Methods: Methods marked virtual define behavior that derived classes follow without redefining the original.
  • ๐Ÿ› ๏ธ Practical Example: The example builds an abstract Tutorial class and a Guru99Tutorial child class in C#.
  • ๐Ÿ“ Design Benefit: Abstract classes enforce a consistent structure across subclasses while allowing specialized functionality.

C# Abstract Class

What is Abstract Class in C#?

Abstract Class can never be instantiated and is marked by the keyword abstract. An abstract class contains zero or more abstract methods in it. Abstract class acts as a base class and is designed to be inherited by subclasses that either implement or either override its method.

Let’s learn abstract class in C# with example given below. Below is the definition of a class called ‘Animal.’ When the ‘Animal’ class is defined, there is nothing known about the animal, whether it is a dog or a cat. The method called description is just a generic method defined for the class.

Abstract Class in C#


Now when it is known what exactly the Animal is going to be, we create another class which inherits the base class. If we know that the animal is in fact a Dog, we create Dog class which inherits the main base class. The key difference here is that the Dog class cannot change the definition of the Description method of the Animal class. It has to define its own C# abstract method called Dog-Description. This is the basic concept of C# abstract classes.

Abstract Class in C#

Create an Abstract Class in C#

Let’s see abstract class in C# with real time examples on how we can change our code to include a C# abstract class. Note that we will not be running the code, because there is nothing that can be run using an C# abstraction class.

Step 1) As a first step, let’s create an abstract class. The class will be called Tutorial and will just have one method. All the code needs to be written in the Program.cs file.

Create an Abstract Class in C#

Code Explanation:-

  1. We first define the abstract class. Note the use of the abstract keyword. This is used to denote that the class is an abstract class.
  2. Next, we are defining our method which does nothing. The method must have the keyword called virtual. This means that the method cannot be changed by the child class. This is a basic requirement for any abstract class.


Step 2) Now let’s add our child class. This code is added to the Program.cs file.

Create an Abstract Class in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 abstract class Tutorial
 {
  public virtual void Set()
  {

  }
 }
  class Guru99Tutorial:Tutorial
  {
   protected int TutorialID;
   protected string TutorialName;

   public void SetTutorial(int pID,string pName)
   {
    TutorialID=pID;
    TutorialName=pName;
   }

   public String GetTutorial()
   {
    return TutorialName;
   }

  static void Main(string[] args)
  {
   Guru99Tutorial pTutor=new Guru99Tutorial();

   pTutor.SetTutorial(1,".Net");

   Console.WriteLine(pTutor.GetTutorial());

   Console.ReadKey();
  }
 }
}

There is nothing exceptional about this code. We just define a class called ‘Guru99Tutorial’ which inherits the abstract Tutorial class. We then define the same methods as we have been using from before.

Note:
Here we cannot change the definition of the Set method which was defined in the Tutorial class. In the Tutorial class, we had defined a method called ‘Set’ (public virtual void Set()). Since the method was part of the abstract class C#, we are not allowed to define the Set method again in the Guru99Tutorial class.

FAQs

An abstract class can hold implemented methods, fields, and constructors, and a class inherits only one. An interface defines only a contract, and a class can implement many interfaces.

Yes. AI coding assistants such as GitHub Copilot can generate abstract classes, suggest method signatures, and create derived classes from a prompt. Developers should still review the output.

AI tutors explain abstraction with tailored examples and analogies, and answer questions instantly. They convert code into plain language, helping beginners grasp how abstract classes hide detail and define shared behavior.

Summarize this post with: