‫In this and the next videos you are going to learn about interfaces and they are quite important because
‫they really extend the functionality of object oriented programming and of classes.
‫So what is an interface?
‫Well, think of interfaces as contracts, where a class that implements an interface agrees to provide
‫implementations for all objects defined by that interface.
‫This means an interface will contain the contract terms, methods and properties, but how we implement
‫them is up to the class that implements the interface.
‫As long as these interface methods are implemented in our class, the interface will remain happy.
‫So interfaces cannot contain any implementations.
‫That's important.
‫So their names are generally prefixed with the IE to distinguish them from other sharp objects.
‫We create interfaces using the interface keyword.
‫Now let's look at how we can do that in practice.
‫All right.
‫So we there we are back in Visual Studio.
‫So one of the most used functionalities in RC Sharp classes is comparing to instances of a class.
‫This is done using the equals method.
‫This will compare to see if the reference to both of the classes is the same.
‫However, we want to compare them and see if they are equal based on some other fields.
‫So we can achieve this by implementing an interface called I Equitable.
‫So in our case, we have a class called Ticket and it defines one property called duration in hours.
‫So let's go ahead and create this class real quick.
‫Here I have a new class and actually I can either go here or use the shortcut control K and C and I'm
‫going to call this one ticket.
‫So this ticket, as I said, just has one property and a constructor.
‫So let's put them in here.
‫So this property is there to store the duration of the ticket and ours.
‫And then we have the constructor here.
‫And now let's go ahead and implement the AI equitable interface.
‫So therefore we use something similar to what we did when using inheritance, we use the colon and then
‫the interface that we want to use.
‫And you see there are a bunch of them.
‫And the one that I want to use is going to be the high equitable.
‫Now you need to define of what type of objects you want to compare.
‫So here I want to compare tickets to each other.
‫This is why I need to add the ticket keyword.
‫Now you will notice that Visual Studio isn't very happy because it says defines a generalized method
‫that a value type or class implements to create a specific method.
‫But then the problem that we get here is ticket does not implement interface members I equitable ticket
‫equals ticket.
‫We can very easily fix that by just going ahead and implementing this.
‫So we could have either used all members well to implement them explicitly or we can just do it manually.
‫Let's do that real quick.
‫So basically I want to return a boolean that will check if whatever ticket I'm passing, which will
‫be the other ticket, is going to be the same as the ticket.
‫That is the one that calls this method.
‫So the ticket.
‫That we create out of this, we compare it to another ticket, and then we want to compare the duration
‫hours.
‫So if they have the same duration, then we will assume that they are the same ticket.
‫So here we can just go ahead and return the comparison of this duration in hours compared to the other
‫duration in hours.
‫So basically we're saying this ticket has a duration in hours and the other ticket is also of type ticket.
‫So it has a duration in hours and we're comparing the two with each other.
‫And this comparison, this double equal sign, will basically return true or false?
‫So this is Boolean operator in left and right.
‫So what it does is it compares what is left to it, to what is right to it.
‫And if they are the same, then it will return true.
‫Otherwise it will return false.
‫Basically saying that this method equals will return true.
‫If the ticket that calls the equals method has the same duration in hours as the ticket that we compare
‫it with, then return true.
‫If not, then return false.
‫Okay, so let's go over to our program, CSE, and actually use this.
‫So here what we're going to create is two tickets, one will be TW one and it will be a new ticket and
‫it will have a duration in hours of ten.
‫And then let's actually copy this and create a ticket two, which will also have 10 hours and now we
‫can compare them.
‫So here let's use control right line to just display tw two dot equals and compare tw one with it.
‫So here I'm basically comparing our ticket two to our ticket one and comparing it using the equals method.
‫Now let's run this real quick.
‫And you will see that it says true.
‫And that is usually not the case for the equals method, because if you compare two objects using a
‫normal equals method, so to speak.
‫So if we were not to implement this, let's get rid of this here for a second.
‫So let's comment all of this out and let's compare the two.
‫You see, the method still is there.
‫This equals still works, because equals is a method that exists in general.
‫And you see now it says false because equals is a method that is inside of the object class.
‫And the object class always has this equals method because every class inherits from object.
‫But now what we're doing is we're implementing our own equals method by using the I equitable interface.
‫So we're using a specific interface that allows us to compare in our own way, so to speak, and implement
‫our own code.
‫So now if we compare the two, we're not comparing if the object is exactly the same that we're comparing
‫it with, but if in our case the time and ours is the same, so the duration in ours is the same, and
‫then it will return true.
‫So if the time and ours is not the same, let's say it's six, then it will return false because the
‫duration is not the same for the two tickets that we're comparing with each other.
‫So this was just an example using a simple interface that exists and as you saw, there are plenty of
‫interfaces that exist automatically when creating a project.
‫But we're going to see how to create our own interfaces and use them in the next video.
‫So see you there.