Understanding C# Abstract Class

What is abstract class?

  1. Abstract class must at least contains one abstract method.
  2. Abstract class can not be instantiated(You can’t create object for a abstract class).
  3. Abstract class can have method with implementations(It is not necessary that all methods need to abstract)
  4. Derived class which inherits the abstract base class must provide implementation for the abstract method.
  5. Abstract class can have constructor even though you can’t create instance for it, but derived class need to pass the values to the base class constructor if base class requires some information( refer my code below)
  6. Abstract class can be used to hold the reference of the derived class object, just like interface(refer my code below)

consider the below example of  abstract class (UserDetails)

Here we have a method called “GetUserName”, we don’t want to provide definition here so we can mark it as abstract. so that the classes which inherits this class must provide the definition.

As you can above, my abstract class contains the constructor even though you can’t create instance for it, but alternatively I passed the value to the base class constructor using “base” from my derived class.

Now we will use these classes in our program,

Hope this helps you to understand the abstract class, please feel free to provide your comments below in the comment section.

 

0 comments… add one

Leave a Comment