Abstract Classes In C#

Today in this section, we will extend Polymorphism by looking at abstract classes. Abstract classes are concept wherein you will never make an instance of base class but derived class. Hence, when you make an abstract class, you also create abstract methods which must be overridden in derived class. Therefore, in a nutshell, Abstract class does the following things:

  • Abstract classes can't be instantiated
  • Must Derive from Abstract class
  • Abstract methods created must get overridden in derived classes.
  • Abstract class can also have non abstract methods with implementation
  • Purpose of abstract classes is clear. It has to create a contract which all the derived classes will implement.
Let us go ahead and see the demo around. Below, I have created simple Abstract class with abstract and non abstract methods to it. I have also used default and Parameterized constructor to set the values in there.
  1. using System;    
  2.     
  3. namespace AbstractClassLibrary    
  4. {    
  5.    public abstract class PaintControls    
  6.     {    
  7.        public int Top { getset; }    
  8.        public int Bottom { getset; }    
  9.        public int Left { getset; }    
  10.        public int Right { getset; }    
  11.     
  12.        //Initializing the values in ctor    
  13.        public PaintControls(int top,int bottom,int left, int right)    
  14.        {    
  15.            Top = top;    
  16.            Bottom = bottom;    
  17.            Left = left;    
  18.            Right = right;    
  19.        }    
  20.     
  21.        //Default ctor just to initialize default values if nothing supplied    
  22.        public PaintControls()    
  23.            :this(0,0,0,0)    
  24.        {}    
  25.     
  26.        //Creating abstract method, which needs to get overriden in derived class    
  27.        //This will not have any body just a signature with semicolon    
  28.        public abstract void drawObject();    
  29.     
  30.        public int returnRandomValue()    
  31.        {    
  32.            Random r = new Random();    
  33.            return r.Next();    
  34.        }    
  35.     }    
  36. }    
Below, I have created new class and simply implemented the same using VS. And, as a result, here is the default implementation for the same.
  1. namespace AbstractClassLibrary    
  2. {    
  3.    public class Button : PaintControls    
  4.     {    
  5.        public override void drawObject()    
  6.        {    
  7.            throw new System.NotImplementedException();    
  8.        }    
  9.     }    
  10. }  
Now, let me go ahead and add few properties to it as well. While doing so, you can observe that base properties coming from abstract class.

Here is the finished class.
  1. using System;    
  2.     
  3. namespace AbstractClassLibrary    
  4. {    
  5.    public class Button : PaintControls     
  6.     {    
  7.        public string ButtonName { getset; }    
  8.     
  9.        public Button(int top,int bottom,int left, int right,string buttonName)    
  10.            :base(top,bottom,left,right)    
  11.        {    
  12.            ButtonName = buttonName;    
  13.        }    
  14.        public override void drawObject()    
  15.        {    
  16.            Console.WriteLine("Drawing object using button: "+ButtonName);    
  17.        }    
  18.     }    
  19. }   
Now, let us go ahead and create object for the same in the main class.
  1. using System;    
  2.     
  3. namespace AbstractClassLibrary    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             Button b = new Button(11, 11, 11, 11, "Line");    
  10.             b.drawObject();    
  11.             //We also called concrete method written in abstract class from derived class object.    
  12.             Console.WriteLine(b.returnRandomValue());    
  13.             Console.ReadLine();    
  14.         }    
  15.     }    
  16. }   
With the above change in place, when I go ahead and run the same, it will produce the following output.



Below stuff is also very much legal. Although, we are having variable of type abstract class but we are creating an instance of derived class.
  1. using System;    
  2.     
  3. namespace AbstractClassLibrary    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             //This is also legal. We are creating a variable of paintcontrols and instantiating Button    
  10.             PaintControls paintControls = new Button(11, 11, 11, 11, "Line");    
  11.             Console.WriteLine(paintControls.returnRandomValue());    
  12.             paintControls.drawObject();    
  13.             Console.ReadLine();    
  14.             //Button b = new Button(11, 11, 11, 11, "Line");    
  15.             //b.drawObject();    
  16.             ////We also called concrete method written in abstract class from derived class object.    
  17.             //Console.WriteLine(b.returnRandomValue());    
  18.             //Console.ReadLine();    
  19.         }    
  20.     }    
  21. }  
With this, it will produce similar output as shown below.



However, the following shown scenario is not legal obviously.


With this, I would like to wrap up this session. Thank you for joining me.

Download Code:

Up Next
    Ebook Download
    View all
    Learn
    View all