Search⌘ K
AI Features

Abstract Classes and Methods

Explore how abstract classes and methods work in C# to promote abstraction and flexible inheritance. This lesson explains key rules, syntax, and practical use through a vending machine example, helping you grasp how to define and implement abstract members in your object-oriented projects.

Abstract Methods

An abstract method is declared using the abstract keyword and does not have a body/ implementation.

There are certain rules we should follow when working with abstract methods. Let’s have a look at these rules.

Rules to be Followed

  • In contrast to a non-abstract/normal C# method, an abstract method does not have a body/definition, i.e., it only has a declaration or method signature.

  • An abstract method can be declared inside an abstract class or an interface only, more on these later.

  • To contain any abstract method in its implementation, a class has to be declared as an abstract class. Non-abstract classes cannot have abstract methods.

  • An abstract method cannot be declared with the private or sealed modifier as it has to be implemented in some other class.

  • Abstract methods are implicitly virtual so we cannot use the keyword virtual in their declaration.

Just like abstract methods, an abstract ...