In previous discussions, we have already seen Interfaces and Abstract Classes in action. Now, let us see live implementation of all three around the same use case. In the end, we will also see the differences between the same. And, then you choose, which is best fit for you.
Let us first discuss simple plain class. Below, I have written simple class to calculate area and perimeter.
Let us first discuss simple plain class. Below, I have written simple class to calculate area and perimeter.
- using System;
- namespace Interface_Demo.Lib
- {
- public class BaseClass
- {
- public int Sides
- {
- get;
- set;
- }
- public int Length
- {
- get;
- set;
- }
- //To fill in these props
- public BaseClass(int sides, int length)
- {
- Sides = sides;
- Length = length;
- }
- //Calculate Perimeter. Number of sides * Length of each side
- public int CalcPerimeter()
- {
- return Sides * Length;
- }
- //Area differs for different object. Hence, it is kept virtual method to get overriden
- //in derived class
- public virtual int CalcArea()
- {
- throw new NotImplementedException();
- }
- }
- }
In this example, I have kept concrete implementation of perimeter calculation but area calculation is virtual method as different objects will have different area calculation, hence it needs to be implemented exclusively in derived class. Now, let us look at the derived class for the same.
- using System;
- namespace Interface_Demo.Lib
- {
- public class Square: BaseClass
- {
- public Square(int sides, int length): base(sides, length)
- {}
- public override int CalcArea()
- {
- return Sides * Length;
- }
- }
- }
Here, virtual method got implemented its way. Now, let's look at main program for the same.
- using Interface_Demo.Lib;
- namespace Interface_Demo.Console
- {
- class Program
- {
- static void Main(string[] args)
- {
- var square = new Square(4, 4);
- DisplayDimensions("Square", square);
- System.Console.ReadLine();
- }
- //dynamic key will resolve an object at runtime. very useful keyword
- private static void DisplayDimensions(string objectType, dynamic square)
- {
- System.Console.WriteLine("Displaying Dimensions of " + objectType);
- System.Console.WriteLine("Number of Sides:- " + square.Sides);
- System.Console.WriteLine("Length of Each Side:- " + square.Length);
- System.Console.WriteLine("Area of " + objectType + " is " + square.CalcArea());
- System.Console.WriteLine("Perimeter of " + objectType + " is " + square.CalcPerimeter());
- System.Console.WriteLine("==============================================================");
- }
- }
- }
Now, when I run the above program, it will produce the following output.

This was just the case with simple plain class. Now, let us look at the same example with abstract class implementation. The following is the abstract class for the same.
Here, in abstract class instead of keeping Virtual method, I just kept abstract method and abstract method cannot have body in base class. Now, let us see it's implementation in derived class.- namespace Interface_Demo.Lib
- {
- public abstract class AbstractBaseClass
- {
- public int Sides
- {
- get;
- set;
- }
- public int Length
- {
- get;
- set;
- }
- //To fill in these props
- public AbstractBaseClass(int sides, int length)
- {
- Sides = sides;
- Length = length;
- }
- //Calculate Perimeter. Number of sides * Length of each side
- public int CalcPerimeter()
- {
- return Sides * Length;
- }
- public abstract int CalcArea();
- }
- }
- namespace Interface_Demo.Lib
- {
- public class SquareAbstract: AbstractBaseClass
- {
- public SquareAbstract(int sides, int length): base(sides, length)
- {}
- public override int CalcArea()
- {
- return Sides * Length;
- }
- }
- }



Joe WilsonPosted Dec 26, 2015, 3:42 AM
Well, it's so good.
Luca Spano'Posted Dec 16, 2015, 5:16 PM
Nice article
Raja TPosted Dec 16, 2015, 10:14 AM
Nice, Thanks for sharing
Kiranteja JallepalliPosted Dec 16, 2015, 9:50 AM
very nice article Rahul Sahay
Maruthi PalllamalliPosted Dec 16, 2015, 9:23 AM
Informative article. When to use Abstact class and When to use Interface ? How Developer can find best situvations for Abstarct class and Interfaces ?
Santhakumar MunuswamyPosted Dec 16, 2015, 9:02 AM
Thanks for nice article. Can you upload source code it will be helpful for the beginner