1 00:00:06,570 --> 00:00:15,360 So in this video, we're going to talk about Question 11, what our default implementations in interfaces, 2 00:00:15,810 --> 00:00:22,260 if I asked you, what are the characteristics of interfaces in C-sharp, you would probably be able 3 00:00:22,260 --> 00:00:24,090 to to them pretty easily. 4 00:00:24,420 --> 00:00:29,430 They can only contain method's properties, indexers and events declarations. 5 00:00:29,700 --> 00:00:31,450 They can't have fields. 6 00:00:31,920 --> 00:00:36,360 The methods declared in the interface can't have implementations. 7 00:00:36,720 --> 00:00:39,960 In other words, there are methods with nobodys. 8 00:00:40,410 --> 00:00:48,270 The methods can be declared abstract or virtual because they are implicitly virtual or its members are 9 00:00:48,270 --> 00:00:54,330 by default public and using, and the other access modifier leads to a compilation error. 10 00:00:54,750 --> 00:00:56,730 They can't have static methods. 11 00:00:57,330 --> 00:00:59,280 This is all perfectly correct. 12 00:00:59,460 --> 00:01:01,230 Or rather, was you. 13 00:01:01,410 --> 00:01:10,800 Prop eight was introduced in 2019 with this version of C-sharp new feature was introduced default implementations 14 00:01:10,800 --> 00:01:12,030 in interfaces. 15 00:01:12,360 --> 00:01:17,070 In short, it means that interfaces can now contain methods we've brought. 16 00:01:17,850 --> 00:01:22,170 Because of that, a couple of other changes must have been introduced to. 17 00:01:22,380 --> 00:01:26,220 For example, methods in the interface can be private now. 18 00:01:26,550 --> 00:01:35,580 An interface can contain fields, etc. If you like me are used to the old interfaces, you are probably 19 00:01:35,580 --> 00:01:36,350 quite surprised. 20 00:01:36,360 --> 00:01:44,240 Now this change goes against everything we knew about interfaces that are an abstraction of our behavior, 21 00:01:44,310 --> 00:01:48,480 or that they only define a contract that a class must fulfill. 22 00:01:49,080 --> 00:01:55,680 When I learned about this challenge, my first thought was So what will be the difference between an 23 00:01:55,680 --> 00:01:58,260 interface and an abstract class now? 24 00:01:58,770 --> 00:02:05,010 It seemed to me and quite correctly, that they will be very similar concepts from now on. 25 00:02:05,310 --> 00:02:10,560 So I was asking myself, why did Microsoft decide to introduce this challenge? 26 00:02:11,100 --> 00:02:15,930 So before we dive into technical details, let's understand why. 27 00:02:16,230 --> 00:02:19,140 I will explain it by showing you an example. 28 00:02:19,500 --> 00:02:26,790 Let's say we develop some library that other people or companies will use will publish it as don't amateur 29 00:02:26,800 --> 00:02:27,360 package. 30 00:02:27,960 --> 00:02:35,400 Let's say this is one of the interfaces we defined in our library, and we expect our customers to provide 31 00:02:35,520 --> 00:02:37,290 their own implementations. 32 00:02:37,710 --> 00:02:45,120 Our library is meant for e-commerce, and this interface defines what functionality should the classes 33 00:02:45,120 --> 00:02:47,130 representing others contain. 34 00:02:47,520 --> 00:02:50,940 We finish our work and we release our library. 35 00:02:51,390 --> 00:02:57,720 The release is a roaring success, and more and more people download the package with Nugget. 36 00:02:58,050 --> 00:03:00,660 It is widely used and highly rated. 37 00:03:01,140 --> 00:03:05,430 One year later, we are almost ready to release version two zero. 38 00:03:05,760 --> 00:03:12,030 There is one problem, though we will try to add console method to the I order interface. 39 00:03:12,360 --> 00:03:19,590 But if we do so and our customers upgrade the version of the library, they will all suddenly see compilation 40 00:03:19,590 --> 00:03:22,200 errors all around the record basis. 41 00:03:22,530 --> 00:03:29,460 The classes they defined to implement the older interface do not provide the implementation of the council 42 00:03:29,460 --> 00:03:30,060 method. 43 00:03:30,240 --> 00:03:36,120 We will force our customers to adjust to this breaking change, and this may not be easy. 44 00:03:36,450 --> 00:03:42,330 Some of them may be stuck with development for days and their business may be impacted by it. 45 00:03:42,870 --> 00:03:47,880 One solution could be to extend the existing interface like this. 46 00:03:56,040 --> 00:04:00,150 We will create a new interface extending the old one. 47 00:04:00,780 --> 00:04:06,960 Our customers can gradually start using it in their code base and everyone is happy. 48 00:04:07,560 --> 00:04:12,390 But there is a problem with this solution as our labor evolves. 49 00:04:12,630 --> 00:04:17,130 We may want to add more and more methods to the older interface. 50 00:04:17,550 --> 00:04:23,970 This will lead to creating new interfaces, and soon it will all become hard to maintain. 51 00:04:24,420 --> 00:04:30,690 No one will wrap their heads around all this work in the application as everyone will see things like 52 00:04:30,690 --> 00:04:33,230 I older, I can celebrate older. 53 00:04:33,450 --> 00:04:36,360 I can celebrate older with delivery delay. 54 00:04:36,570 --> 00:04:44,760 I consider older with delivery, delay and discount, etc. And this is where default implementation 55 00:04:44,760 --> 00:04:46,590 in interfaces can help. 56 00:04:46,980 --> 00:04:54,450 We can add new methods to an existing interface and provide default implementations so we won't break 57 00:04:54,450 --> 00:04:57,130 our customers code if they want to. 58 00:04:57,210 --> 00:05:03,450 They can provide their own implementation, but until then, the default implementation will be used. 59 00:05:03,660 --> 00:05:05,280 Let's see this in code. 60 00:05:14,170 --> 00:05:20,860 As you can see, the delayed delivery by Decommitted has a body which was impossible before C-sharp 61 00:05:20,860 --> 00:05:21,340 eight. 62 00:05:21,760 --> 00:05:25,900 Now let's define a class implementing this interface. 63 00:05:27,810 --> 00:05:34,200 And here it is, as you can see, it doesn't provide the implementation of the delayed delivery by this 64 00:05:34,200 --> 00:05:34,740 method. 65 00:05:35,040 --> 00:05:39,920 Let's see if we can call this method on an object of the custom order glass. 66 00:05:45,320 --> 00:05:47,330 Well, that doesn't compile. 67 00:05:47,570 --> 00:05:53,390 We can't use the default interface implementations on the variables of a concrete type. 68 00:05:53,570 --> 00:05:59,540 We must use it via the interface and now it works fine. 69 00:05:59,720 --> 00:06:02,240 And this implementation will be used. 70 00:06:02,900 --> 00:06:08,420 Of course, if we provide the implementation of this method in the concrete glass, it will be used 71 00:06:08,420 --> 00:06:11,270 instead of the implementation from the interface. 72 00:06:11,570 --> 00:06:15,590 Let's add another glass implementing the AI order interface. 73 00:06:21,260 --> 00:06:24,950 As you can see, I changed the implementation of this method. 74 00:06:30,690 --> 00:06:33,360 Let's see what will be printed to the council. 75 00:06:34,870 --> 00:06:39,790 As you can see, if the loan default implementation is provided, it will be used. 76 00:06:41,350 --> 00:06:42,100 All right. 77 00:06:42,220 --> 00:06:45,400 Since now we can define methods and interfaces. 78 00:06:45,580 --> 00:06:52,630 We may need some other things that typically are used in methods like other private methods that can 79 00:06:52,630 --> 00:06:54,430 enclose some piece of logic. 80 00:06:54,790 --> 00:07:01,750 Static methods to do the same if they don't use another static members of the interface also private 81 00:07:01,750 --> 00:07:02,320 fields. 82 00:07:03,160 --> 00:07:07,900 Let's summarize the topic of default implementations in interfaces. 83 00:07:08,230 --> 00:07:11,020 This feature was added in C Sharp eight. 84 00:07:11,380 --> 00:07:17,980 It's mostly designed to make it easier to add new methods to interfaces without breaking the existing 85 00:07:17,980 --> 00:07:18,700 code. 86 00:07:18,880 --> 00:07:27,040 Also, it makes it possible for C-sharp to work with apps targeting Android written in Java and iOS. 87 00:07:27,220 --> 00:07:34,690 Written in Swift as those languages support similar features, they also enable using something called 88 00:07:34,690 --> 00:07:35,390 traits. 89 00:07:35,470 --> 00:07:37,510 What is beyond this conscious level? 90 00:07:37,690 --> 00:07:44,530 I think some article about them in the resources attached to this lecture are the default implementations 91 00:07:44,530 --> 00:07:51,850 in interfaces was a huge turnout, and it completely altered what was true about interfaces in C-sharp. 92 00:07:52,390 --> 00:07:59,740 This feature received a lot of criticism, and to be honest, I can see why the line between interfaces 93 00:07:59,740 --> 00:08:02,620 and up six classes is very, very now. 94 00:08:02,920 --> 00:08:09,810 In practice, it's hard to provide a default implementation that brings up the value in the resources. 95 00:08:09,820 --> 00:08:16,900 I linked our extensive article pointing out some problems with the default implementations in interfaces. 96 00:08:17,350 --> 00:08:19,660 My recommendation is as follows. 97 00:08:20,110 --> 00:08:25,300 Be aware that something like the default implementation of interfaces exist. 98 00:08:25,600 --> 00:08:32,390 Still, I think it's best if you use interfaces as they were meant to be used before C-sharp eight, 99 00:08:32,440 --> 00:08:38,980 unless you are 100 percent sure you know what you're doing and equally sure that this will bring value 100 00:08:39,010 --> 00:08:40,150 to the application. 101 00:08:40,950 --> 00:08:47,310 During the interview, you can be asked what can be the reason for using different implementations in 102 00:08:47,310 --> 00:08:48,360 interfaces? 103 00:08:48,930 --> 00:08:55,140 Different implementations in interfaces are mostly designed to make it easier to add new methods to 104 00:08:55,140 --> 00:08:59,740 interfaces without breaking the existing code without it. 105 00:08:59,760 --> 00:09:06,420 If we automated one interface and we release it as a public library will force everyone who updates 106 00:09:06,420 --> 00:09:09,730 this library to provide the implementation immediately. 107 00:09:10,110 --> 00:09:13,020 Otherwise, the code will not build. 108 00:09:13,440 --> 00:09:14,160 All right. 109 00:09:14,190 --> 00:09:15,660 That's it about this topic. 110 00:09:15,810 --> 00:09:18,720 Thanks for watching and see you in the next lecture.