Extending Interfaces

Hi Friends,

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.

Continue reading “Extending Interfaces” »

1,011 total views, 1 views today

Class Vs Abstract Class Vs Interfaces

Hi Friends,

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. 22nd

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.

23rd

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.

24th

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.

Thanks,
Rahul Sahay
Happy Coding

1,398 total views, 1 views today

Interfaces

Hi Friends,

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.

17th Below is the main class in its finished form.

Now, when I run the same, it will produce the following output.

20th

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.

18th

With the above change in place, it will print the following output.

19th

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.

21th

Download Link:- https://github.com/rahulsahay19/InterfaceDemo

With this, I would like to wrap this session here. Thanks for joining me.

Thanks,
Rahul Sahay
Happy Coding

922 total views, 1 views today

Abstract Classes

Hi Friends,

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.

13th

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.

14th

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.

15th

However, Below shown scenario is not legal obviously.

16th

With this, I would like to wrap up this session. Thanks for Joining Me.

Download Code :- https://github.com/rahulsahay19/AbstractClass

Thanks,
Rahul Sahay
Happy Coding

1,234 total views, no views today

Method Overloading

Hi Friends,

In previous section, we have seen Inheritance and one kind of Polymorphism that is Overriding. In this section, we will delve further and see how Method Overloading works.

Method Overloading follows a set of rules. They are:-

  • Different Number of Parameters
  • Different Types of Parameters

One more important thing to understand here, signature of method means its name and parameter list not the return type.

Also, it’s important to understand difference between Overloading and Overriding.

  • Overloading means same method name with different parameters or types of parameters in the same class.
  • Overriding means virtual methods gets overridden in derived class.

Now, let’s look at the demo for the same. Below, I have added Overloaded method.

Now, let me go ahead and call the same from the main program as shown below.

With the above change in place, When I run the program, then it will produce the following output.

11th

Since, this method is virtual, hence I can override this as well in the derived class as shown below.

Then, in the main class.

12th

Which means, here method got overloaded and also overridden.

I hope you would have enjoyed this session. We’ll delve further in coming section. Till then, stay tuned and Happy coding.

Download Link:- https://github.com/rahulsahay19/csharp-Basics

Thanks,
Rahul Sahay
Happy Coding

2,178 total views, 1 views today

Practical Inheritance

Hi Friends,

In this section, we will talk about practical implementation of Inheritance. Inheritance basically explains IS-A relationship. We will see the same in a moment. Below, I have created simple Doctor class which inherits from DoctorInfo.

Now, the 1st thing which I would like to do with this class is to create a constructor as shown below. 3rd Continue reading “Practical Inheritance” »

1,004 total views, no views today

Simple Multiple Inheritance Example

Hi Friends,

In Today’s example we will see some basics. Here, in the below example a sample implementation of multiple interface getting implemented explicitly in a class.

Thanks,
Rahul
Happy Coding

929 total views, no views today