Abstraction In C#


The word abstract means a concept or an idea not associated with any specific instance.
In C# programming, we apply the same meaning of abstraction by making classes not associated with any specific instance. Abstraction is needed when we need to only inherit from a certain class, but do not need to instantiate objects of that class. In such a case the base class can be regarded as "Incomplete". Such classes are known as an "Abstract Base Class". 

Abstract Base Class

There are some important points about Abstract Base Class :
  1. An Abstract Base class cannot be instantiated; it means the object of that class cannot be created.
  2. Class having the abstract keyword with some of its methods (not all) is known as an Abstract Base Class.
  3. Class having the Abstract keyword with all of its methods is known as pure Abstract Base Class.
  4. The method of the abstract class that has no implementation is known as "operation". It can be defined as an abstract void method ();
  5. An abstract class holds the methods but the actual implementation of those methods is made in derived class.
Let's have a look at this code!
  1. abstract class animal {    
  2.     public abstract void eat();    
  3.     public void sound() {    
  4.         Console.WriteLine("dog can sound");    
  5.     }    
  6. }  
This is the Abstract Base Class, if I make both of its methods abstract then this class would become a pure Abstract Base Class.
Now, we derive a class of 'dog' from the class animal.
  1. abstract class animal {    
  2.     public abstract void eat();    
  3.     public void sound() {    
  4.         Console.WriteLine("dog can sound");    
  5.     }    
  6. }    
  7. class dog: animal {    
  8.     public override void eat() {    
  9.         Console.WriteLine("dog can eat");    
  10.     }    
  11. }   
Here you can see we have 2 methods in the Abstract Base Class, the method eat() has no implementation; that is why it is being declared as 'abstract' while the method sound() has its own body so it is not declared as 'abstract'.
In the derived class, we have the same name method but this method has its body.
We are doing abstraction here so that we can access the method of the derived class without any trouble.
Let's have a look!
  1. class program  
  2. {  
  3.     abstract class animal  
  4.     {  
  5.         public abstract void eat();  
  6.         public void sound()  
  7.         {  
  8.             Console.WriteLine("dog can sound");  
  9.         }  
  10.     }  
  11.     class dog : animal  
  12.     {  
  13.         public override void eat()  
  14.         {  
  15.             Console.WriteLine("dog can eat");  
  16.         }  
  17.     }  
  18.     static void Main(string[] args)  
  19.     {  
  20.         dog mydog = new dog();  
  21.         animal thePet = mydog;  
  22.         thePet.eat();  
  23.         mydog.sound();  
  24.     }  
  25. }  
Finally, we created an Object 'mydog' of class dog, but we didn't instantiate any object of Abstract Base Class 'animal'.
According to "Ivor Horton" (a programmer of Java) an object can not be instantiated, but we can declare a variable of the Abstract Class type. If this statement is true then it could be possible:
animal thePet;
This is an object which is declared as thePet and its data type is the abstract base class 'animal'.
We can use this Object to store Objects of the subclass.
In the above code, we declare an Object 'thePet', of the type animal (the Abstract Base Class) and simply copied the object of another object (only the reference is copied as they belong to reference type). Now we can use object 'thePet' just as object 'mydog'.
The output of this code would be as,
Abstract Base Class in C#  


l conclude here by saying that Abstraction is not a difficult job to do, but you need to be confident while performing abstraction. Every new topic covers all the previous topics. In abstraction, polymorphism is covered and performed. The method overriding could be done by putting the keyword 'new' before that overridden method. Everything is possible in Programming and there are multiple ways to do a single job. Abstraction is one of the smart ways to do this kind of tasks.

Recommended Ebook

LINQ Quick Reference with C#

Download Now!
Similar Articles