Skip to main content

Command Palette

Search for a command to run...

C# Abstract

Updated
6 min read
C# Abstract

Introduction

Hello all, today I back with another C# topic, and today I will share what i learned about C# Abstract. I hope with this article you will know more about C# Abstract and use it on your project.

What It Is?

An abstract class acts as a blueprint for derived classes that inherit from it. By defining a class as abstract, you essentially create a class that is purely a design, without an actual implementation. This means that it's impossible to create an object of an abstract class, as the term "blueprint" suggests only represents the design and not the concrete implementation.

By defining a class as abstract, you can now create different derived classes that retain the same characteristics of the abstract class but with distinct implementations.

Why Implement It?

There are several pros and cons to consider before implement an abstract in our project. Here are some of them:

Pros

  • Enhance code reusability.

  • Enhance code scalability.

Cons

  • Could potentially increase code complexity.

  • Subclass tightly coupled to the abstract class.

  • It may be challenging for new programmers to understand.

How It Works?

To understand how abstract work in the system, let's implement it first. To implement an abstract, all you need to do is add an abstract modifier to your class, method, or property, as shown in the code below:

namespace Lncodes.Example.Abstract;

public abstract class EnemyController
{
    protected abstract int Stamina { get; set; }

    public abstract void Run();
}

In the code above, I have created an abstract class for enemy. Within this class, I create an abstract property called Stamina and an abstract method named Run(). After implementing this abstract in the code, here's what will happen in the system:

  1. When creating an abstract class, the system will allow the creation of either an abstract method or an abstract property within the abstract class.

  2. When creating an abstract class, the system will prohibit the creation of an instance of that class.

  3. When creating an abstract method or abstract property, the system will automatically attach a virtual keyword to it in the background. As a result, an override keyword is necessary to modify the method or property in the derived class.

  4. When creating a method or property with an abstract modifier in the parent class, all derived classes are required to provide an actual implementation to that method or property. This is because methods or properties with abstract modifiers have no actual implementation.

Console Application

In my console application, an abstract class is used to create a blueprint for all types of enemies. Each enemy type will have different stamina points and unique implementations for how they perform run and rest operations.

Below is a list of the classes I used to create this console application, along with a brief explanation of each class:

ClassDescription
EnemyControllerThis class is used as a blueprint for each type of enemy.
TrollEnemyController, OrcEnemyController, GoblinEnemyControllerThese classes are used to implement the blueprint members of the EnemyController abstract class.
ProgramThis class is used to display information about a random enemy and show all the actions that the enemy performs in the console application.

In the video above, you'll notice that each enemy type has its own unique default stamina points. Additionally, the stamina points used while running and gained while resting are different for each enemy type.

By implementing an abstract class, creating a new types of enemies has become significantly easier, thanks to the established blueprint of the EnemyController class.

The source code for this console application can be viewed and downloaded at Project Repository – Github.

Additional Information

I have discovered some additional information about C# Abstract. If you have any additional information regarding C# Abstract that you'd like to share, please feel free to leave a comment.

Abstract VS Interface

Both abstract classes and interfaces offer similar functionality for enhancing code abstraction by defining contracts or blueprints for derived classes. Due to these similarities, choosing between them can be challenging. Here are some key factors to consider before making a decision:

  • Use an abstract class when the members functionality are exclusively used within a single module. For instance, when creating the EnemyController abstract class, we limited its functionality to be exclusively used within the enemies module, not for players or NPC. This exclusivity makes it ideal to use an abstract class rather than an interface.

  • Abstract class can have methods with default implementations, but interface can't have methods with default implementations.

    Since C# 8, interface can have methods with default implementations. However, the object of the derived class can't call this method unless the derived class overrides it first.
  • All access modifiers can be used in the abstract class members, but only the public access modifier can be used in an interface members.

    Since C# 8, interface members can use the private, protected, or internal access modifier.
  • Abstract class can have variables as a member, but interface can't have them as a member.

  • A class can't inherit from multiple abstract classes, but it can inherit from multiple interfaces.

  • A struct can't inherit from an abstract class, but it can inherit from an interface.

💡
To learn more about C# Interface, check out my post at C# Interface - Last Night Codes.

Abstract Method VS Virtual Method

Both abstract methods and virtual methods offer similar functionality in providing flexible method implementations for derived classes. Due to these similarities, choosing between them can be challenging. Here are some key factors to consider before making a decision:

  • Use the abstract method when the majority of the methods in the derived class have different implementations. However, if only a minority of the derived classes requires a different implementation, use virtual instead of abstract.

  • When using abstract method, it's mandatory to override the method in the derived class. Conversely, there is no requirement to override a virtual method in the derived class. This is because abstract method don't have implementation in the parent class but virtual method do.

Further Discoveries

Here are some further discoveries I have gathered from various sources that may provide a deeper understanding of C# Abstract:

  • The abstract class can't be instantiated.

  • An abstract class can have a constructor, but never makes the constructor has a public access modifier because abstract class can’t be initialized. Instead use public access modifier make the constructor use protected or internal access modifier instead.

  • An interface or an abstract class can inherit from another abstract class.

  • Using a sealed modifier on the abstract class will not cause a compiler error.

    Since C# 8, the compiler will display an error message when a sealed modifier is used in an abstract class.

Reference

  1. C# Abstract – Microsoft

C#

Part 7 of 7

In this series, I’ll share the most important lessons and discoveries I’ve acquired about C#. From fundamental concepts to advanced features, discover how these lessons have sharpened my coding skills and how they can benefit to my projects.

Start from the beginning

C# Asynchronous

Introduction Hello all, today I back with another C# topic, and today I will share what i learned about C# Asynchronous. I hope with this article you will know more about C# Asynchronous and use it on your project. What Is It? Microsoft introduced as...

More from this blog

L

Last Night Codes

11 posts

Last Night Codes shares my journey through learning different programming languages and documenting my projects. Explore practical tips, insights, and personal coding experiences.