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.
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.
Here, virtual method got implemented its way. Now, let’s look at main program for the same.
This was just the case with simple plain class. Now, Let us look the same example with abstract class implementation. Below 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.
Fairly simple and clean. Similarly, main program for the same, now looks like
With the above change in place, when I run the same, then it will produce the following output.
Similarly, we can implement the same stuff using interface. Below is the sample Interface for the same and it’s implementation.
Now, the main program for the same will look like as shown below.
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 if 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.
Download link:- https://github.com/rahulsahay19/Interface-Abstract-Demo
I hope you would have liked this small discussion around the same. Thanks for joining me.
1,479 total views, 1 views today