Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C# Abstract Classes: A Practical Tutorial with Code Examples

What Is an Abstract Class in C#?

An abstract class serves as a blueprint for other classes. It can never be instantiated directly and is marked with the abstract keyword. Abstract classes typically contain one or more abstract or virtual methods that subclasses are expected to implement or override. They provide a common contract while allowing concrete subclasses to supply specific behavior.

Consider the following example. We first define a generic Animal class without knowing whether the animal will be a dog, cat, or anything else. The Description method is a placeholder that will be specialized by derived classes.

C# Abstract Classes: A Practical Tutorial with Code Examples

When the specific type of animal is known, we create a subclass such as Dog that inherits from Animal. The Dog class cannot alter the base Description method; instead, it provides its own implementation or adds new methods like DogDescription. This demonstrates the core concept of C# abstract classes: a base contract with concrete subclasses filling in the details.

C# Abstract Classes: A Practical Tutorial with Code Examples

Creating an Abstract Class in C#

Below is a step‑by‑step guide to defining an abstract class and a concrete subclass that uses it. While we won’t execute this snippet in an online editor, the code demonstrates best practices for abstract class design.

Step 1: Define the Abstract Class

We create an abstract class named Tutorial containing a single virtual method. The virtual keyword indicates that the method cannot be overridden by derived classes.

C# Abstract Classes: A Practical Tutorial with Code Examples

Code Explanation

  1. We declare the class with the abstract keyword to prevent direct instantiation.
  2. Inside the class, the Set method is defined as public virtual void Set() so that subclasses inherit it unchanged.

Step 2: Create the Derived Class

In the same file, we define a concrete class Guru99Tutorial that inherits from Tutorial. The subclass introduces its own properties and methods while respecting the contract established by the abstract base.

C# Abstract Classes: A Practical Tutorial with Code Examples

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()
        {
            // Base implementation (currently empty)
        }
    }

    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();
        }
    }
}

The key point is that Guru99Tutorial cannot redeclare the Set method defined in Tutorial; it must use the inherited version as is.

Summary

Abstract classes in C# provide a robust foundation for building extensible systems. By declaring common behavior in a non‑instantiable base class, developers enforce a contract while allowing derived classes to supply concrete implementations. This pattern promotes code reuse, clarity, and strong typing across large codebases.

C Language

  1. C++ Classes & Objects: A Practical Guide with Code Examples
  2. Master C# Classes & Objects: A Practical Tutorial with Real‑World Examples
  3. Understanding C# Interfaces: Definition, Example, and Practical Use
  4. C# Windows Forms Development: A Hands‑On Tutorial
  5. Mastering Abstraction in OOP: Java Abstract Classes & Methods Explained
  6. Understanding Java Classes and Objects: Clear Concepts, Practical Examples
  7. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  8. Java Interfaces Explained: How to Define and Implement Them with Practical Examples
  9. Master Java Reflection API: A Practical Guide with Code Examples
  10. Java Abstraction Explained: Simplify Code & Boost Readability