When to use Abstract Class in c#?

Manjula Jayanthi
2 min readMay 1, 2020

--

Most of us always confused between these two modifiers Sealed and Abstract. Though i mentioned as class in title, it is necessary to get from origin, that is Modifiers.

We must understand Sealed and Abstract are working exact opposite direction to each other.

Sealed(to prevent from inherited)←🙅🏻→Abstract(exposed to be inherited conditionally)

Can applied on Methods/Class/Properties.

What is Abstract?

Wherever I see this term, it remains me implementation is incomplete and without any choice it is a Base class to other class(except a scenario abstract class can be a child class of another abstract class). Let us see with example,

from above we can understand below facts,

  1. Abstract class can have Abstract/NonAbstract methods or properties
  2. At the same time, get to remember NonAbstract Class cannot have any Abstract methods or properties
  3. Abstract methods or properties implemented using override modifier in derived class
  4. It is bad race to use virtual modifier in abstract method. There we should understand virtual allows us to override a method in inherited class. The same function is done by abstract. Basically abstract method is implicitly a virtual method.

It is necessary to know how Abstract class behaves as Child class, as I mentioned at initial.

Notice `ChildClass`(Abstract) is inherited from `HomeClass`(Abstract).

From these content let us conclude When to use Abstract?

  1. When same method/accessor/class needed in more classes with different implementation
  2. When you want to avoid more instantiation
  3. When you want to make sure, all the methods of Base Class were implemented in inherited class

This is my first story in medium.Thank you for reading.

--

--

No responses yet