Abstract Classes in C#

Abstract Classes in C#
 
The meaning of abstraction is, you don't need to explain everything. In our real life, we have vehicles such as cars, motorcycles and scooters. People only know how to drive these vehicles. It's not necessary to know how the vehicle runs or how petrol or diesel works in that vehicle.
 
So when we define an abstract we don't need to explain everything. It's a type of class, but you cannot create an instance (object) of it. In other words you cannot create an instance of the class. Now you might ask why do we not create an instance of an abstract class? The answer is that when we create or define an abstract class, it's not complete.
 
You must inherit this class and define its methods and properties on this new derived class.
 
Now I'll show you how all this is done. Have a look.
 
For the purposes of learning OOP concepts, Console applications are more convenient so I am using a Console Application.
  1. // File Name :- Abstractcls.cs  
  2. abstract  class Abstractcls  
  3. {  
  4.        //An Example of how to define a Abstract Method.  
  5.         public abstract  int Add(int a, int b, int c);  
  6. }   
One thing is to always remember that when you define an abstract function, always use your access specifier as public. Because private and protected are not able to use it.
  1. //FileName:- Maths.cs  
  2. class Maths:Abstractcls  
  3. {  
  4.     public override int Add(int a, int b, int c)  
  5.     {  
  6.         return a + b + c;  
  7.     }  
  8.     static void Main(string[] args)  
  9.     {  
  10.         //Creating objects of Maths class  
  11.         Maths _objMaths = new Maths();  
  12.         Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));  
  13.         Console.ReadLine();  
  14.     }  
  15. }
If you are a beginner in C# or do not understand console applications then here I want to explain Console.Writeline. This is used to output something. The meaning of {0} is you get the value of the first object in the argument list. So here {0} is the index of your first object. Here your object is _objMaths.
 
We can only inherit one abstract class for a class. In an interface we can do this, because inheritance is for multiple inheritance. In an abstract class, there is no rule for multiple inheritance.
 
Now we can learn how to implement properties in an abstract class, and override it in a derived class.
  1. // File Name:- Abstractcls.cs     
  2. abstract class Abstractcls  
  3. {  
  4.     //An Example of how to define a Abstract Method.  
  5.     public abstract int Add(int a, int b, int c);  
  6.     // x is protected variable  
  7.     protected int x = 7;  
  8.     //Define Abstract Property  
  9.     public abstract int val  
  10.     {  
  11.         get;  
  12.     }  
  13. }   
  1. // File Name:- Math.cs  
  2. // Here Abstract class Inherit a derrive class Maths  
  3. class Maths:Abstractcls  
  4. {  
  5.     //Override Add Method  
  6.     public override int Add(int a, int b, int c)  
  7.     {  
  8.         return a + b + c;  
  9.     }  
  10.     //Override of val Property  
  11.     public override int val  
  12.     {  
  13.         get  
  14.         {  
  15.             return x;  
  16.         }  
  17.     }  
  18.     static void Main(string[] args)  
  19.     {  
  20.         //Creating objects of Maths class  
  21.         Maths _objMaths = new Maths();  
  22.         Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));  
  23.         //getting the value of val Property  
  24.         Console .WriteLine ("Abstract Properties:={0}",_objMaths.val );  
  25.         Console.ReadLine();  
  26.     }  
  27. }
We can add simple methods to an abstract class. You cannot override simple methods in a derrived class.
  1. //File Name: Abstractcls  
  2. abstract class Abstractcls  
  3. {  
  4.     //Simple Method in an abstract class  
  5.     public int minuse(int a, int b)  
  6.     {  
  7.         return a - b;  
  8.     }  
  9. }  
  1. //File Name:- Math.cs  
  2. class Maths:Abstractcls  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.         //Creating objects of Maths class  
  7.         Maths _objMaths = new Maths();  
  8.         Console.WriteLine("Override String fun-{0}",_objMaths .minuse (10,4));  
  9.     }  
  10. }  
FAQ on Abstraction
 
Question 1: Can only abstract methods, properties or indexers be put in the abstract class?
 
Answer: No, you can write abstract methods and normal methods and properties for an abstract class, but you cannot override it (normal methods).
  1. // File Name:- Abstractcls.cs  
  2. abstract class Abstractcls  
  3. {  
  4.     //Simple Method in abstract class  
  5.     public int minuse(int a, int b)  
  6.     {  
  7.         return a - b;  
  8.     }  
  9. }  
  1. //File Name:- Math.cs  
  2. class Maths:Abstractcls  
  3. {  
  4.     static void Main(string[] args)  
  5.     {  
  6.         //Creating objects of Maths class  
  7.         Maths _objMaths = new Maths();  
  8.         Console.WriteLine("Override String fun-{0}",_objMaths .minuse (10,4));  
  9.     }  
  10. }  
Question 2: Can we use a Private or Protected access specifier with an abstract method?
 
Answer: No, you can use only public and the internal access specifier for an abstract class, method or property.
 
Question 3: Can we create an instance of an abstract class and if not then why?
 
Answer: We can't create an instance of an abstract class because an abstract class is incomplete. You only provide the signatures of properties, methods, events and indexers. An abstract class is always inherited by a simple class, as you saw in the above examples.
 
Question 4: Can we inherit an abstract base class in the derrived class?
 
Answer: Yes. You can do this. But you must override it in a derrived abstract class.
  1. //File Name:- Abs1.cs  
  2. abstract  class abs1  
  3. {  
  4.     public abstract int div(int a, int b);  
  5. }  
  6. //File Name :- Abstractcls.cs  
  7. abstract class Abstractcls:abs1  
  8. {  
  9.     public abstract int Add(int a, int b, int c);  
  10.   
  11.     //Here i override abs1 base class  
  12.     public override int div(int a, int b)  
  13.     {  
  14.         return a / b;  
  15.     }  
  16. }
  1. //File Name:- Math.cs  
  2. class Maths:Abstractcls  
  3. {  
  4.     //Override Add Method  
  5.     public override int Add(int a, int b, int c)  
  6.     {  
  7.         return a + b + c;  
  8.     }  
  9.   
  10.     static void Main(string[] args)  
  11.     {  
  12.         //Creating objects of Maths class  
  13.         Maths _objMaths = new Maths();  
  14.         Console.WriteLine("Abstract function result={0}",_objMaths .Add (10,20,30));  
  15.         Console.Write("Override int {0}=",_objMaths .div (10,2));  
  16.         Console.ReadLine();  
  17.     }  
  18. }


Similar Articles