Abstract Class in C#

Introduction 

An abstract class is generally called an incomplete class.

Why do we call it an incomplete class?

It consists of at least one member that may be a method or property that has a signature, but doesn't have an implementation or body.

For example:

A function will be declared in an abstract class, but logic that should be provided in that function is unknown. This will be left incomplete and that is the reason we consider it to be an incomplete class and when it is an incomplete class you cannot create an instance of that class.

Classes that are in reality marked as an abstract class cannot be defined properly. We know that a class has this feature, but how does that feature behave, what feature will it consist of, cannot it be defined properly? In such cases, we mark it as an abstract class.

The best example of this is a Car.

Assume I asked you to create a class called Car and specify all the features in the form of members (function/property). Then, I would like you to describe the features of a car, then how will you describe it.

For Example:

 

  1. it moves on the road
  2. wheels=4
  3. steering=1
  4. doors=?, there I got stuck.

Is 4 doors compulsory in every car?

Does all the cars have 4 doors? Normally they have 4 doors, but if you take a sports car, it has only 2 doors.

Example

The Reva car is an electric car that has only 2 doors.

So, what should I specify, doors=what?

Some very long cars exist in the US that has 6 doors, but the number of doors will vary. So, what number should I provide? I can't, a car has doors, but how many I can't say, it differs, its incomplete. If that is incomplete, then it becomes an abstract class. You know that it has a feature, but you're not sure how that feature functions and behaves.

So how is it that we will provide the implementation for the number of doors? The derived class will decide how many doors there are for the class.

  1. using System;  
  2. abstract class car   
  3. {  
  4.     public int Wheels   
  5.     {  
  6.         get   
  7.         {  
  8.             return (4);  
  9.         }  
  10.     }  
  11.     public void Moves()   
  12.     {  
  13.         Console.WriteLine("moves on the road");  
  14.         Console.WriteLine("uses fuel to move");  
  15.     }  
  16.     public abstract int Doors   
  17.     {  
  18.         get;  
  19.     }  
  20. }  
  21. class Reva: car   
  22. {  
  23.     public override int Doors   
  24.     {  
  25.         get   
  26.         {  
  27.             return (2);  
  28.         }  
  29.     }  
  30. }  
  31. class Demo   
  32. {  
  33.     static void Main()   
  34.     {  
  35.         Reva obj = new Reva();  
  36.         obj.Moves();  
  37.         Console.WriteLine("Number of Wheels:" + obj.Wheels);  
  38.         Console.WriteLine("Number of Doors:" + obj.Doors);  
  39.     }  
  40. }  
Output

moves on the road
uses fuel to move
Number of Wheels:4
Number of Doors :2

Advance points:

Some of my friends get a doubt ? This question will ask in interviews also .
Suppose i will not declare doors property at all in the base class(car), i will give the implementation in derived class.
Is there any problem? this is the question most of the interviews will ask,will this not work?
In that case no need to declare as an abstract class ,it can be a normal class when they inheritance is done there if doors are there you mention that doors=2.
The reason is No,
 
you should not do that way ,if you are not sure about that feature if that is going to be decided by the derived class you have to declare.
For Example :1
Suppose you have declare it in base class(doors property ),but  the derived class doesn't mention doors=2 then you will get a compilation error saying that the abstract member is there, it is not give a body you have to give the body.
It will force you to give the body , then you will say doors=2.
For Example :2
Suppose if you are not declare it in base class you are saying that should be given by derived class ,if the derived class programer has forgot to mention the doors then no complication error ,program will compile, reva car is produced it is released into the market but it doesn't has doors that is the problem.beacause you are not declare it in base class you don't no so you thought that it will defined by derived class(reva).
dervied class person forgot to mention so when you are not sure you declare it and it will be forced by derived class(reva) they have to give implementation.
 

Up Next
    Ebook Download
    View all
    Learn
    View all