It gives me immense satisfaction writing and dedicating this post to all my readers, who always gave me valuable and honest feedback for the posts. Because of you people only, This year I Received Microsoft MVP Award. This year has been exciting journey for me. I learned lots of new things and shared the same with you guys as well. I’ve been making my year-end “best of” lists since couple of days. When I started making my lists, almost nobody else online was making lists, I feel so. Then I realize that the internet was a place where you could make a list about things that happened in a year, and then share that list with people whom you admire and love most. And I capitalized on that realization.
So why am I bringing up this history?
I always believe, this is one of the best way to remind yourself, that still destination is miles ahead. This was just the beginning and with interim success, one should not get settle down. Also, I have seen when it comes to lists, we have a problem, and we usually say I want to propose a solution. I feel that when people look at a year-end list — whether it’s a best-of list, or a worst-of list, or a this-is-what-happened-this-year, it should show you actual picture like what you did correct or wrong. In either case there is always a room for improvement.
Therefore, whatever you guys are doing in whatever field do it full-heartedly and try to give your best. Now, this post is really getting tough for me as philosophy is not my area of interest. In the end, I will just say, Keep learning, Happy Learning and Happy New Year.
In this section, we will delve further with Interfaces and see Why Interfaces are important for any Scalable, Extend-able and Testable. And, one of the key reasons which I believe basically for using Interfaces is to keep the code future proof and Maintainable. Now, let us consider a below case. Here, I have plain simple class with few properties in it. Now, this will serve as model for me populating the values.
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.
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 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.
In Today’s section, we’ll talk about Interfaces. Interfaces are basically constraints which Implementing class must follow. In one way, it is similar to abstract class but doesn’t act as base class. C# doesn’t have multiple inheritance. You can inherit from only one class but you can implement as many interfaces as you like. Also, one point to understand that access modifiers like private, public, protected etc are not legal in case of interfaces. Hence, without wasting time, let’s jump at demos.
Below is the simple interface in its plain form.
Now, let us go ahead and create one class which implements the same. Below is the default implementation for the same. You may also notice that since I am not having setter in interface, hence in Implementation that came as private setter which means value can be set from constructor.
Below is the modified code for class implementation.
Now, let us go ahead and use the same in main class. While writing the class, you can see available properties and methods to get exposed.
Below is the main class in its finished form.
Now, when I run the same, it will produce the following output.
However, It is also perfectly legal to have a variable of type Interface and instantiate a class like shown below. But, while doing so, here I won’t be having method access which is declared and implemented explicitly in class.
With the above change in place, it will print the following output.
Also, we can have main method like shown below.
Here, I have used cast operator as to check whether the object is of type class or interface. And, this will produce the following output.
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.
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.
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.
Now, let us go ahead and create object for the same in the main class.
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.
With this, It will produce similar output as shown below.
However, Below shown scenario is not legal obviously.
With this, I would like to wrap up this session. Thanks for Joining Me.