1 00:00:01,280 --> 00:00:06,710 ‫In this and the next videos you are going to learn about interfaces and they are quite important because 2 00:00:06,710 --> 00:00:12,890 ‫they really extend the functionality of object oriented programming and of classes. 3 00:00:13,010 --> 00:00:15,140 ‫So what is an interface? 4 00:00:15,170 --> 00:00:22,490 ‫Well, think of interfaces as contracts, where a class that implements an interface agrees to provide 5 00:00:22,490 --> 00:00:25,940 ‫implementations for all objects defined by that interface. 6 00:00:26,180 --> 00:00:33,500 ‫This means an interface will contain the contract terms, methods and properties, but how we implement 7 00:00:33,500 --> 00:00:36,890 ‫them is up to the class that implements the interface. 8 00:00:36,950 --> 00:00:42,800 ‫As long as these interface methods are implemented in our class, the interface will remain happy. 9 00:00:43,580 --> 00:00:47,150 ‫So interfaces cannot contain any implementations. 10 00:00:47,150 --> 00:00:48,110 ‫That's important. 11 00:00:48,170 --> 00:00:54,630 ‫So their names are generally prefixed with the IE to distinguish them from other sharp objects. 12 00:00:54,650 --> 00:00:58,520 ‫We create interfaces using the interface keyword. 13 00:00:58,670 --> 00:01:01,790 ‫Now let's look at how we can do that in practice. 14 00:01:02,640 --> 00:01:02,940 ‫All right. 15 00:01:02,940 --> 00:01:05,410 ‫So we there we are back in Visual Studio. 16 00:01:05,430 --> 00:01:12,040 ‫So one of the most used functionalities in RC Sharp classes is comparing to instances of a class. 17 00:01:12,060 --> 00:01:14,370 ‫This is done using the equals method. 18 00:01:14,400 --> 00:01:18,990 ‫This will compare to see if the reference to both of the classes is the same. 19 00:01:19,080 --> 00:01:25,230 ‫However, we want to compare them and see if they are equal based on some other fields. 20 00:01:25,440 --> 00:01:30,930 ‫So we can achieve this by implementing an interface called I Equitable. 21 00:01:31,110 --> 00:01:38,600 ‫So in our case, we have a class called Ticket and it defines one property called duration in hours. 22 00:01:38,610 --> 00:01:41,160 ‫So let's go ahead and create this class real quick. 23 00:01:41,310 --> 00:01:47,820 ‫Here I have a new class and actually I can either go here or use the shortcut control K and C and I'm 24 00:01:47,820 --> 00:01:49,530 ‫going to call this one ticket. 25 00:01:49,980 --> 00:01:54,660 ‫So this ticket, as I said, just has one property and a constructor. 26 00:01:54,660 --> 00:01:56,010 ‫So let's put them in here. 27 00:01:56,020 --> 00:01:59,940 ‫So this property is there to store the duration of the ticket and ours. 28 00:01:59,940 --> 00:02:03,180 ‫And then we have the constructor here. 29 00:02:04,230 --> 00:02:08,460 ‫And now let's go ahead and implement the AI equitable interface. 30 00:02:08,640 --> 00:02:16,320 ‫So therefore we use something similar to what we did when using inheritance, we use the colon and then 31 00:02:16,320 --> 00:02:17,970 ‫the interface that we want to use. 32 00:02:17,970 --> 00:02:19,800 ‫And you see there are a bunch of them. 33 00:02:20,070 --> 00:02:23,700 ‫And the one that I want to use is going to be the high equitable. 34 00:02:24,000 --> 00:02:29,030 ‫Now you need to define of what type of objects you want to compare. 35 00:02:29,040 --> 00:02:32,250 ‫So here I want to compare tickets to each other. 36 00:02:32,430 --> 00:02:34,960 ‫This is why I need to add the ticket keyword. 37 00:02:34,980 --> 00:02:42,300 ‫Now you will notice that Visual Studio isn't very happy because it says defines a generalized method 38 00:02:42,300 --> 00:02:46,110 ‫that a value type or class implements to create a specific method. 39 00:02:46,110 --> 00:02:51,720 ‫But then the problem that we get here is ticket does not implement interface members I equitable ticket 40 00:02:51,720 --> 00:02:52,500 ‫equals ticket. 41 00:02:53,500 --> 00:02:58,590 ‫We can very easily fix that by just going ahead and implementing this. 42 00:02:58,600 --> 00:03:04,790 ‫So we could have either used all members well to implement them explicitly or we can just do it manually. 43 00:03:04,810 --> 00:03:06,000 ‫Let's do that real quick. 44 00:03:06,010 --> 00:03:14,470 ‫So basically I want to return a boolean that will check if whatever ticket I'm passing, which will 45 00:03:14,470 --> 00:03:19,000 ‫be the other ticket, is going to be the same as the ticket. 46 00:03:19,000 --> 00:03:21,070 ‫That is the one that calls this method. 47 00:03:21,340 --> 00:03:22,330 ‫So the ticket. 48 00:03:23,110 --> 00:03:28,990 ‫That we create out of this, we compare it to another ticket, and then we want to compare the duration 49 00:03:28,990 --> 00:03:29,260 ‫hours. 50 00:03:29,260 --> 00:03:33,100 ‫So if they have the same duration, then we will assume that they are the same ticket. 51 00:03:33,730 --> 00:03:41,590 ‫So here we can just go ahead and return the comparison of this duration in hours compared to the other 52 00:03:41,590 --> 00:03:43,000 ‫duration in hours. 53 00:03:43,030 --> 00:03:49,390 ‫So basically we're saying this ticket has a duration in hours and the other ticket is also of type ticket. 54 00:03:49,390 --> 00:03:53,290 ‫So it has a duration in hours and we're comparing the two with each other. 55 00:03:53,290 --> 00:03:59,530 ‫And this comparison, this double equal sign, will basically return true or false? 56 00:04:00,040 --> 00:04:02,950 ‫So this is Boolean operator in left and right. 57 00:04:02,950 --> 00:04:07,060 ‫So what it does is it compares what is left to it, to what is right to it. 58 00:04:07,060 --> 00:04:10,000 ‫And if they are the same, then it will return true. 59 00:04:10,030 --> 00:04:11,740 ‫Otherwise it will return false. 60 00:04:11,740 --> 00:04:15,250 ‫Basically saying that this method equals will return true. 61 00:04:15,250 --> 00:04:22,060 ‫If the ticket that calls the equals method has the same duration in hours as the ticket that we compare 62 00:04:22,060 --> 00:04:23,830 ‫it with, then return true. 63 00:04:23,830 --> 00:04:25,480 ‫If not, then return false. 64 00:04:26,220 --> 00:04:30,330 ‫Okay, so let's go over to our program, CSE, and actually use this. 65 00:04:30,340 --> 00:04:37,840 ‫So here what we're going to create is two tickets, one will be TW one and it will be a new ticket and 66 00:04:37,840 --> 00:04:40,210 ‫it will have a duration in hours of ten. 67 00:04:40,210 --> 00:04:47,560 ‫And then let's actually copy this and create a ticket two, which will also have 10 hours and now we 68 00:04:47,560 --> 00:04:48,580 ‫can compare them. 69 00:04:48,580 --> 00:04:59,050 ‫So here let's use control right line to just display tw two dot equals and compare tw one with it. 70 00:04:59,050 --> 00:05:06,400 ‫So here I'm basically comparing our ticket two to our ticket one and comparing it using the equals method. 71 00:05:06,880 --> 00:05:08,440 ‫Now let's run this real quick. 72 00:05:09,820 --> 00:05:11,920 ‫And you will see that it says true. 73 00:05:12,160 --> 00:05:17,710 ‫And that is usually not the case for the equals method, because if you compare two objects using a 74 00:05:17,710 --> 00:05:19,480 ‫normal equals method, so to speak. 75 00:05:19,510 --> 00:05:25,360 ‫So if we were not to implement this, let's get rid of this here for a second. 76 00:05:25,840 --> 00:05:29,830 ‫So let's comment all of this out and let's compare the two. 77 00:05:29,860 --> 00:05:31,690 ‫You see, the method still is there. 78 00:05:32,500 --> 00:05:37,900 ‫This equals still works, because equals is a method that exists in general. 79 00:05:37,900 --> 00:05:44,380 ‫And you see now it says false because equals is a method that is inside of the object class. 80 00:05:44,380 --> 00:05:51,040 ‫And the object class always has this equals method because every class inherits from object. 81 00:05:51,370 --> 00:05:59,710 ‫But now what we're doing is we're implementing our own equals method by using the I equitable interface. 82 00:05:59,710 --> 00:06:07,600 ‫So we're using a specific interface that allows us to compare in our own way, so to speak, and implement 83 00:06:07,600 --> 00:06:08,560 ‫our own code. 84 00:06:08,560 --> 00:06:13,810 ‫So now if we compare the two, we're not comparing if the object is exactly the same that we're comparing 85 00:06:13,810 --> 00:06:21,130 ‫it with, but if in our case the time and ours is the same, so the duration in ours is the same, and 86 00:06:21,130 --> 00:06:22,480 ‫then it will return true. 87 00:06:22,480 --> 00:06:28,450 ‫So if the time and ours is not the same, let's say it's six, then it will return false because the 88 00:06:28,450 --> 00:06:32,740 ‫duration is not the same for the two tickets that we're comparing with each other. 89 00:06:32,890 --> 00:06:38,680 ‫So this was just an example using a simple interface that exists and as you saw, there are plenty of 90 00:06:38,680 --> 00:06:43,150 ‫interfaces that exist automatically when creating a project. 91 00:06:43,150 --> 00:06:48,520 ‫But we're going to see how to create our own interfaces and use them in the next video. 92 00:06:48,520 --> 00:06:49,870 ‫So see you there.