• We just launched and are currently in beta. Join us as we build and grow the community.

Abstract Classes in C# Language

Troupe13

Data Lake Architect
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
112
Likes
118
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. protected

    Vehicle(

    )

    {

    }


  5. public

    Vehicle(

    int

    wheels)
  6. {
  7. this

    .

    wheels

    =

    wheels;
  8. }

  9. /*
  10. * this class abstract member of the abstract class
  11. */

  12. public

    abstract

    void

    Operate(

    )

    ;

  13. public

    void

    increaseSpeed(

    int

    v)
  14. {
  15. speed +=

    v;
  16. }

  17. /*
  18. * this is virtual member that the derived class can optionally choose to override
  19. * to change the operation
  20. */

  21. public

    virtual

    void

    decreaseSpeed(

    int

    v)
  22. {
  23. speed -=

    v;
  24. }

  25. }

  26. public

    class

    Car :

    Vehicle {

  27. /*
  28. * this class implements an abstract member from abstract class
  29. */
  30. public

    override

    void

    Operate(

    )
  31. {
  32. Console.

    WriteLine

    (

    "this is implement member"

    )

    ;
  33. }

  34. /*
  35. * this class member overrides the virtual member of base class.
  36. */

  37. public

    override

    void

    decreaseSpeed(

    int

    v)
  38. {
  39. speed -=

    v /

    2

    ;
  40. }

  41. }

    ;

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.

Book traversal links for Abstract Classes in C# Language

  • ‹ Working with List in C#
  • Up
  • Basic Regular Expression in C# ›

 

452,292

323,526

323,535

Top