Abstract Classes in C# Language

Objective

Beside Inheritance which has provides a lot of benefit, in C# we can also provided with abstract class and interface. This tutorial will show you with the definition and how to use them to make your program more flexible.

Let's go

Abstract classes

There are times when you need to provide derived classes with a standard implementation and need to guarantee that a derived class provides an implementation for a particular property or method, or simply makes sense to have a class that can have no instances By declaring a class as abstract, you prevent it from being instantiated directly. As a result, abstract classes typically have protected constructor rather than public ones. If you don’t provide a default constructor, the compiler creates a protected default constructor for you. An abstract class can contain virtual, non-virtual and abstract members (known as methods). You need to note that:

  • An abstract member is declared with the abstract modifier but does not provide an implementation.
  • An abstract member is only declared in an abstract class.
  • An abstract class can has a non-abstract member.
Listing bellow shows an example of the Vehicle class as an abstract class containing an abstract method named Operate.
  1. public abstract class Vehicle
  2. {
  3. int wheels;
  4.  
  5. protected Vehicle() {}
  6.  
  7.  
  8. public Vehicle(int wheels)
  9. {
  10. this.wheels = wheels;
  11. }
  12.  
  13. /*
  14.   * this class abstract member of the abstract class
  15.   */
  16.  
  17. public abstract void Operate();
  18.  
  19. public void increaseSpeed(int v)
  20. {
  21. speed += v;
  22. }
  23.  
  24. /*
  25.   * this is virtual member that the derived class can optionally choose to override
  26.   * to change the operation
  27.   */
  28.  
  29. public virtual void decreaseSpeed(int v)
  30. {
  31. speed -= v;
  32. }
  33.  
  34. }
  35.  
  36. public class Car : Vehicle {
  37.  
  38. /*
  39.   * this class implements an abstract member from abstract class
  40.   */
  41. public override void Operate()
  42. {
  43. Console.WriteLine("this is implement member");
  44. }
  45.  
  46. /*
  47.   * this class member overrides the virtual member of base class.
  48.   */
  49.  
  50. public override void decreaseSpeed(int v)
  51. {
  52. speed -= v / 2;
  53. }
  54.  
  55. };

Eplanation

In the above example, we declared an abstract Vehicle class. In this class, this class has three functions/members:

  • Abstract member Operate(): this member requires all derived class excepts abstract derived class implements a member with name Operate.
  • A non-virtual member named increaseSpeed that increases speed with value “v”. This is member that’s not required to implement by a derived class.
  • A virtual member named decreaseSpeed that decreases speed with value “v”. This is member that a derived class can optionally override to change the operation.

If the derived class is also abstract, id does not need to override a base class abstract member. Because an abstract member has no implementation until overridden in a derived class, it is not possible for that overridden member to call the same member form the base class.

Summary

In this tutorial we have show you about Abstract class definition, includes:

  • Explanation about abstract class, what abstract class is and why should you use it.
  • An simple example of how to declare and how to use abstract class in program.
Using abstract class in the right way can help you to make conformation for programming, and also provide flexibility when modifying operation of code. But try to use it in a clear context to avoid make you program complex.

Comments

Submitted byAnonymous (not verified)on Fri, 04/30/2021 - 11:11

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Abstraction {

Add new comment