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.
- using System;
-
- namespace AbstractClassLibrary
- {
- public abstract class PaintControls
- {
- public int Top { get; set; }
- public int Bottom { get; set; }
- public int Left { get; set; }
- public int Right { get; set; }
-
-
- public PaintControls(int top,int bottom,int left, int right)
- {
- Top = top;
- Bottom = bottom;
- Left = left;
- Right = right;
- }
-
-
- public PaintControls()
- :this(0,0,0,0)
- {}
-
-
-
- public abstract void drawObject();
-
- public int returnRandomValue()
- {
- Random r = new Random();
- return r.Next();
- }
- }
- }
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.
- namespace AbstractClassLibrary
- {
- public class Button : PaintControls
- {
- public override void drawObject()
- {
- throw new System.NotImplementedException();
- }
- }
- }
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.
- using System;
-
- namespace AbstractClassLibrary
- {
- public class Button : PaintControls
- {
- public string ButtonName { get; set; }
-
- public Button(int top,int bottom,int left, int right,string buttonName)
- :base(top,bottom,left,right)
- {
- ButtonName = buttonName;
- }
- public override void drawObject()
- {
- Console.WriteLine("Drawing object using button: "+ButtonName);
- }
- }
- }
Now, let us go ahead and create object for the same in the main class.
- using System;
-
- namespace AbstractClassLibrary
- {
- class Program
- {
- static void Main(string[] args)
- {
- Button b = new Button(11, 11, 11, 11, "Line");
- b.drawObject();
-
- Console.WriteLine(b.returnRandomValue());
- Console.ReadLine();
- }
- }
- }
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.
- using System;
-
- namespace AbstractClassLibrary
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- PaintControls paintControls = new Button(11, 11, 11, 11, "Line");
- Console.WriteLine(paintControls.returnRandomValue());
- paintControls.drawObject();
- Console.ReadLine();
-
-
-
-
-
- }
- }
- }
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: