C# Abstract classes

C# Abstract Classes Tutorial with Example

What is an Abstract Class in C#?

ABSTRACT CLASS can never be instantiated and is marked by the keyword abstract. An abstract class should have a minimum of one abstract method. Abstract class acts as a base class and is designed to be inherited by subclasses that either implement or either override its method.

A typical example of an abstract class is 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.

C# Class and Object

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 method called Dog-Description. This is the basic concept of abstract classes.

C# Class and Object

Let’s see how we can change our code to include an abstract class. Note that we will not be running the code, because there is nothing that can be run using an abstract 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.

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.

C# Class and Object

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) 
   public String GetTutorial()
    return TutorialName;
  static void Main(string[] args) 
   Guru99Tutorial pTutor=new Guru99Tutorial();

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, we are not allowed to define the Set method again in the Guru99Tutorial class.



  • An abstract class is a base class which has the very basic requirements of what a class should look like. It is not possible for the child class to inherit the methods of the base class.



Glad to have you at Our Website
Welcome to WPBot