Okay, so up to this point we are done with three principles, single responsible principle, open closed principle, and liskov substitution principle. Now it's a time for moving ahead to the fourth one, which is interface segregation principle. And to understand this principle, we just have to add one interface first. So let me take one scenario. If I go back to my Visual Studio, inside my Visual Studio, I'm going to add a new interface now. So I'm going to add a new item and this is going to be an interface and we are giving the name of this interface, I invoice spring.
Now, as per the naming C sharp, it's a good habit that you always add one eye before the name of the interface. So I'm following the same naming convention. And we have an invoice printer which is going to be my public interface. Like always whenever we have an interface we are going to have some of the method signatures in that I hope you all know that interface is going to be an abstraction so you cannot have a meta logic inside that. So assume that we have one method which is having a name print, which is taking one parameter of type invoice and this invoice parameter name we are giving invoice only. So this is my one method which is returning nothing, that's why it's void and assume that we have one more method.
So, we are just changing the name of this methods properly. So this method name is print and name of this method is going to be print with the logo. So, we have two different methods assume that some of our clients are looking for a print method and some wants to print an invoice with a logo actually. So we have two different methods inside this interface. Now, if I want to use this interface for a printing method and all this thing, then we need to take one particular class which can implement this interface. So assume that we have a new item, which is going to be a class.
And this class is going to maybe having a name bring thing system. So this printing system class is going to allow me to print as I like. So, I'm going to implement the interface I invoice printer inside that. And now whenever I want to use an interface, I have to right click on this and I have two quick action in these options which is implement interface. Now when I click on implement interface, we know that is going to give me all the methods of that interface. And I need to write a logic inside all of these methods.
Now assume that if I have a client who just want to take a print method and they do not want to take print with logo or in the reverse case They want to take print with logo they do not want to take print method. In this case, my interface is forcing me to use both the methods I cannot take only one. For example, if I remove this one, it's going to start showing me an error inside this that printing system does not implement the interface, which is going to have a method with the name print with logo. This is something which is my principle number four, and if you focus on interface segregation principle saying that this one liner for this is any interface should not force any method to a class which is implementing it. So, they are saying that any interface should not force any method like in our case my I invoice print interface is actually forcing both the methods inside this.
Now, if I did not want that my interface should not force then I should have only limited number of methods inside there which are required. For example, my invoice print interface is having print and print with logic. Now if some of the clients wants to use only one method, it's not possible in this case because it's going to force both. The solution of this thing is I need to remove this method from this. So I'm going to do Ctrl x. And now I invoice print is only having one method which is print, which is implementing here.
Now I'm going to add one more interface. So I'm going to add I add new item. And this is going to be an interface. Let's say the name of the interface we are giving I invoice sorry, they're giving I print with logo. So they are giving I print with logo. That's the new interface which we are adding and this interface is going to have a method which is for print with logo.
So now we have two different interfaces I invoice print, and we have I print with logo. Now both of these interfaces are actually having one one method. So inside In any particular class, if I want to use only one method, I can implement the interface and I can use that. While it's also possible that suppose I have a new client so I'm adding a new class. Suppose this is my printing printing system for let's say client two. So in this new class, which is printing system for client two, we are assuming that like printing system is only looking for one letter print which is coming from I invoice print.
Maybe I have a client, they want to take a normal print as well as they want to utilize the new functionality which is printing invoices with logo. Now if they want to utilize both, the only thing is because interface support multiple inheritance, we can specify that this class is going to first take I invoice print So from that we can get a normal print method. And then we can even implement one more interface inside this, which is I print with logo. And the moment we implement this interface, we'll get the second method. So this time is also following interface segregation principle, because this interface is only giving me one method and the other interface if you need another method, and none of the interfaces are forcing any extra methods into my class. If any particular client wants to use only one, they can use one.
And if somebody wants both the functionality, we can take the help of multiple inheritance and we can take both interfaces into that particular class. And we can write the logic. So we can have a print logic which is a simple print logic inside this. And same way we can have an additional print logic which is going to be a print logic with logo. And when we do this thing, it's going to have both logics inside printing system client to while printing system is just going to have One particular logic for normal printing. Now, in this case, we are following interface segregation principle, which is telling us that any particular interface should not force any extra methods into the class.
And that's why we are keeping one one interface with only one simple method inside that. Sometime even people say this thing that interface segregation principle is very much similar to single responsibility principle. The only difference is single responsibility principle talks about a focus purpose for a particular class, while in this case, interface segregation principle talks about one single focus purpose with one single method inside interface. I hope you're getting what I'm saying. Thank you.