VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/c-sharp-abstract-classes/

⇱ Abstract Class in C# - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Abstract Class in C#

Last Updated : 16 Sep, 2025

In C#, an abstract class is a class that cannot be instantiated directly. Abstract classes are used when we want to define a common template for a group of related classes but leave some methods or properties to be implemented by derived classes.

  • An abstract class cannot be directly instantiated. We can only create objects of derived classes.
  • Abstract methods are declared in the abstract classes but do not have implementation, derived classes are required to implement them.
  • An abstract class also contains properties and fields which can be accessed by derived classes.

Example


Output
The Cat goes Meow
Walking like a cat...
The Dog goes Woof
Running like a dog...

Explanation:

  • Animal is declared as an abstract class, so it cannot be instantiated.
  • It contains an abstract property Sound that must be implemented by all derived classes.
  • It also has a virtual method Move() with a default implementation that can be overridden.
  • Cat and Dog override both Sound and Move() to provide their own behavior.
  • In Main, Animal references hold Cat and Dog objects, showing polymorphism.
  • This demonstrates how abstract classes define a blueprint with both mandatory and optional behaviors.

Declaration of Abstract Classes

abstract class gfg{}
// class 'gfg' is abstract

Key Points

  • Generally, we use abstract class at the time of inheritance.
  • A derived class must use the override keyword to implement an abstract method.
  • It can contain constructors or destructors.
  • It can implement functions with non-Abstract methods.
  • It cannot support multiple inheritances.
  • It can’t be static.

Example 1: Working of an abstract class.


Output
class Child1
class Child2

Example 2: This example demonstrates abstract classes with both abstract and non-abstract methods, where the non-abstract method is inherited directly and the abstract method is overridden in the derived class.


Output
Addition: 10
Multiplication: 24

Note: An abstract method is a method that is declared in an abstract class but has no body. Any non-abstract class that inherits the abstract class must provide the implementations for the abstract method.

Example 3: Program to calculate the area of a Square using abstract class and abstract method


Output
Area = 36

Example 4: Abstract class can also work with get and set accessors.


Output
5

Advantages

  • Encapsulation: Defines common behavior without exposing implementation details.
  • Code reuse: Serves as a base for multiple classes, reducing duplication.
  • Polymorphism: Enables working with different derived classes through a shared base.

Disadvantages

  • Tight coupling: Changes in base class can affect all derived classes.
  • Limited inheritance: Only one abstract base class can be inherited.
  • Testing difficulty: Cannot be instantiated directly, requiring mocks or stubs.
Comment

Explore