What is Abstract class in C#?

In today's post i am going to cover "What is Abstract class in C#  "? and later we understand the diffrence between Abstract class and Interface.

Abstract class in C#

Abstract Class

Abstract class is a class which can not be instantiated.It means object creation for that class is not possible.The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.(To improve the functionality of the method).

A class is marked with abstract keyword to declared as abstract class.Below is the simple example of abstract class.


Abstract class can contain either non-abstract method(concrete method) or abstract method.Abstract members do not have any implementation in the abstract class, but the in derived class it should be implemented.

Key points to remember:

  • An abstract class can have abstract member as well as non-abstract method (concrete method).
  • Abstract keyword is used to declare the method as abstract in abstract class.
  • A class can inherit more than one interface but only one abstract class.
  • An abstract class can have public, protected, static methods, but the abstract method in abstract class cannot be static.
  • An abstract method cannot be private.
  • An abstract method cannot have the modifier virtual because an abstract method is implicitly virtual.
  • The access modifier of abstract method should be same in both the abstract class and derived class.
  • When a class contains at least one abstract method, then the class must be declared as abstract class.
  • It is mandatory to override abstract method in derived class.
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:


If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method .In the previous example, GetData() on class D cannot call GetData() on class A. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.


Hope you enjoyed this post.Please provide your feedback.


Thanks.
What is Abstract class in C#? What is Abstract class in C#? Reviewed by CodiBucket on 05:46 Rating: 5

No comments:

Facebook

Powered by Blogger.