What Is An Abstract Class?

What is an abstract class?

Abstract class is an Object Oriented programming based concept. In simple words, abstract means a non-physical thought, idea, or concept. An abstract class acts as only a base class, which means an abstract class is incomplete. An abstract class is complete with derived class or other child class.

Object oriented programming concept - what is an encapsulation

Purpose of an abstract class


Here, we have different types of cars and trucks. All vehicles have different colors, design and engine types which make them different from each other but they have something in  common as all of them have engines, tiers, gears, steering etc. and are used for traveling.

What should be an abstract concept? Vehicle

As shown in the figure given above, vehicle can be a general concept for car and truck. A vehicle provides general information related to car and truck.

Abstract class declaration:

An abstract class is created using “ABSTRACT” keyword before class name.

Example


Abstract class can include both abstract and non-abstract members.

Example


Now, we can create one derived class car to implement abstract method but when we implement an abstract method in derived class, use override keyword before Method name, as shown below.


Now we can create an object in main class but keep in mind, we cannot create an object directly for an abstract class. When any ’abstract class’ is inherited by any other class, we create the objects of that derived class.


Output


Example code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace OPPs  
  8. {  
  9.     abstract class Truck  
  10.     {  
  11.         //Non-Abstract Member   
  12.         public void Color()  
  13.         {  
  14.             Console.WriteLine("Base Class : Vehicle Color");  
  15.         }  
  16.         //Abstract Member   
  17.         public abstract void Model();  
  18.     }  
  19.     class Car : Truck  
  20.     {  
  21.         public override void Model()  
  22.         {  
  23.             Console.WriteLine("Derived : Vehicle Type Sedan car ");  
  24.         }  
  25.     }  
  26.      class Program  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             Car objCar = new Car();  
  31.             objCar.Color();  
  32.             objCar.Model();  
  33.             Console.ReadLine();  
  34.         }  
  35.     }     
  36. }  

An abstract class is a class, which is used for sharing common functionality to all its derived classes and with the help of an abstract class, it is very easy to change all the related methods into the derived class that can be inherited from the base class without disturbing all other derived classes. In simple words, for code reusability or to avoid code duplication, it is used. 

Key Points

  • An abstract class cannot be instantiated.
  • An abstract method can only be declared in an abstract class.
  • All derived classes must implement all the abstract methods in it.
  • We cannot create object of abstract class. 
  • We cannot use sealed modifier.
  • An abstract method is by default; a virtual.
  • An abstract class is designed to act as a base class.
  • Avoid code duplication.

I discussed why an abstract class is important in an Object Oriented programming language.

Have a nice day.

Next Recommended Reading Relation Between Classes