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.
- using System;
- namespace Interface_Demo.Lib
- {
- public class BaseClass
- {
- public int Sides
- {
- get;
- set;
- }
- public int Length
- {
- get;
- set;
- }
-
- public BaseClass(int sides, int length)
- {
- Sides = sides;
- Length = length;
- }
-
- public int CalcPerimeter()
- {
- return Sides * Length;
- }
-
-
- 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();
- }
-
- 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.
- namespace Interface_Demo.Lib
- {
- public abstract class AbstractBaseClass
- {
- public int Sides
- {
- get;
- set;
- }
- public int Length
- {
- get;
- set;
- }
-
- public AbstractBaseClass(int sides, int length)
- {
- Sides = sides;
- Length = length;
- }
-
- public int CalcPerimeter()
- {
- return Sides * Length;
- }
- public abstract int CalcArea();
- }
- }
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 class SquareAbstract: AbstractBaseClass
- {
- public SquareAbstract(int sides, int length): base(sides, length)
- {}
- public override int CalcArea()
- {
- return Sides * Length;
- }
- }
- }
Fairly simple and clean. Similarly, main program for the same, now looks like the following,
- using Interface_Demo.Lib;
- namespace Interface_Demo.Console
- {
- class Program
- {
- static void Main(string[] args)
- {
- var square = new Square(4, 4);
- DisplayDimensions("Square", square);
- var squareAbstract = new SquareAbstract(4, 4);
- DisplayDimensions("Square with abstract Implementation", squareAbstract);
- System.Console.ReadLine();
- }
-
- 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("==============================================================");
- }
- }
- }
With the above change in place, when I run the same, it will produce the following output,
Similarly, we can implement the same stuff using interface. The following is the sample interface for the same and it's implementation.
- namespace Interface_Demo.Lib
- {
- public interface ICircle
- {
- int radius
- {
- get;
- set;
- }
-
- double CalcPerimeter();
- double CalcArea();
- }
- }
- namespace Interface_Demo.Lib
- {
- public interface ICircle
- {
- int radius
- {
- get;
- set;
- }
-
- double CalcPerimeter();
- double CalcArea();
- }
- }
- namespace Interface_Demo.Lib
- {
- public class CircleInterface: ICircle
- {
- public int radius
- {
- get;
- set;
- }
- int ICircle.radius
- {
- get
- {
- return radius;
- }
- set
- {
- radius = value;
- }
- }
- public CircleInterface(int rad)
- {
- radius = rad;
- }
- public double CalcPerimeter()
- {
- return (2 * 3.14 * radius);
- }
- public double CalcArea()
- {
- return (3.14 * radius * radius);
- }
- }
- }
Now, the main program for the same will look like the following,
- using Interface_Demo.Lib;
- namespace Interface_Demo.Console
- {
- class Program
- {
- static void Main(string[] args)
- {
- var square = new Square(4, 4);
- DisplayDimensions("Square", square);
- var squareAbstract = new SquareAbstract(4, 4);
- DisplayDimensions("Square with abstract Implementation", squareAbstract);
- var circleInterface = new CircleInterface(5);
- DisplayCircle("Circle with Interface Implementation", circleInterface);
- System.Console.ReadLine();
- }
- private static void DisplayCircle(string objectType, CircleInterface circleInterface)
- {
- System.Console.WriteLine("Displaying Dimensions of " + objectType);
- System.Console.WriteLine("Circle with Radius:- " + circleInterface.radius);
- System.Console.WriteLine("Area of " + objectType + " is " + circleInterface.CalcArea());
- System.Console.WriteLine("Perimeter of " + objectType + " is " + circleInterface.CalcPerimeter());
- System.Console.WriteLine("==============================================================");
- }
-
- 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("==============================================================");
- }
- }
- }
With the above change in place, it will produce the following output,
Therefore, in this section, we have seen all three in action. Now, let us discuss few important points around these three.
- Plain Class:
Frankly speaking, the disadvantage of simple class or plain class is, it doesn't enforce to implement the virtual methods in the derived class. Therefore, at compile time it won't produce any error, but if by chance you call the same method in main program, then at run time it will throw exception.
- Abstract Class:
It enforces to implement the abstract method in the derived class, else we will get compile time error, which is good obviously. However, Abstract classes can contain concrete methods with implementations as well. Now, in C#, one class can get inherited from only one class as c# doesn't support multiple inheritance. Members of abstract classes can have access to access modifiers like public, private, protected, etc. Also, abstract classes can contain anything like Events, Fields, Properties, Constructors, Destructors, Methods, Indexers.
- Interfaces:
It also enforces compile time checking. You need to implement all the methods upfront. Interface can't have any implementations inside. It can only have declarations. Also, a class can implement multiple interfaces. Interfaces by default are public. Hence, if we try to add any access modifier to any interface member, we will get compile time error. But, interfaces can have Methods, Events, Indexers or Properties.
Code Download link:
I hope you liked this small discussion around the same. Thanks for joining me.