1 00:00:01,590 --> 00:00:06,240 ‫In this lesson, you will learn the critical difference between abstract classes and interfaces. 2 00:00:06,330 --> 00:00:11,400 ‫When designing applications, it is essential to know when to use an abstract class and when to use 3 00:00:11,400 --> 00:00:14,100 ‫an interface and when to use neither of them. 4 00:00:14,580 --> 00:00:20,790 ‫Abstract classes and interfaces seem similar in some ways, but there are key differences which will 5 00:00:20,790 --> 00:00:25,080 ‫determine which is the best choice for what you are trying to achieve. 6 00:00:25,470 --> 00:00:30,840 ‫Understanding the difference between an abstract class and an interface is very important, so we can 7 00:00:30,840 --> 00:00:37,200 ‫be able to design loosely coupled and extensible applications, so applications that we can modify and 8 00:00:37,200 --> 00:00:39,570 ‫change easily without breaking everything. 9 00:00:39,780 --> 00:00:44,790 ‫We will discuss those differences and how to decide when to use which in this video. 10 00:00:44,850 --> 00:00:50,250 ‫But before we do that, first, let's refresh our memory about abstract classes and interfaces. 11 00:00:50,520 --> 00:00:52,080 ‫So first, abstract classes. 12 00:00:52,080 --> 00:00:58,320 ‫An abstract class is an incomplete class or a class that can't be instantiated, which means we cannot 13 00:00:58,320 --> 00:00:59,610 ‫create objects from it. 14 00:00:59,910 --> 00:01:07,620 ‫The purpose of an abstract class is to provide a blueprint for derived classes and to set some rules 15 00:01:07,620 --> 00:01:10,680 ‫like what the derived classes must implement. 16 00:01:11,010 --> 00:01:15,420 ‫But why would we need a class to be marked as an abstract class to begin with? 17 00:01:15,600 --> 00:01:17,730 ‫What's wrong with regular classes? 18 00:01:18,030 --> 00:01:23,730 ‫Imagine we have a class motorcycle and a class car and they share some functionality to reduce code 19 00:01:23,730 --> 00:01:24,630 ‫redundancy. 20 00:01:24,810 --> 00:01:33,060 ‫We create a basic class called vehicle to prevent creating instances of type vehicle and to enforce 21 00:01:33,060 --> 00:01:36,960 ‫every method to implement their version of the vehicle methods. 22 00:01:36,960 --> 00:01:40,050 ‫We have to mark it as an abstract class. 23 00:01:40,320 --> 00:01:46,830 ‫We can take advantage of abstract classes to design components and specify some level of common functionality 24 00:01:46,830 --> 00:01:49,620 ‫that must be implemented by derived classes. 25 00:01:50,130 --> 00:01:53,970 ‫So let's make this vehicle abstract and this will be fine. 26 00:01:54,120 --> 00:01:56,280 ‫Now let's look at interfaces once again. 27 00:01:56,280 --> 00:02:01,740 ‫So an interface is a contract and it can contain only method declarations. 28 00:02:01,800 --> 00:02:04,980 ‫It cannot contain method definitions. 29 00:02:05,280 --> 00:02:08,160 ‫They define an ability that a class has. 30 00:02:08,190 --> 00:02:14,460 ‫Let's imagine we have three classes, so we have the class, bicycle, motorcycle and car. 31 00:02:14,460 --> 00:02:20,790 ‫And then we want to make sure that all of these classes inherit from this class vehicle. 32 00:02:20,970 --> 00:02:26,310 ‫And if you want to add self driving functionality to our car, for example, and we want you to create 33 00:02:26,310 --> 00:02:31,860 ‫other classes, for example, that might create something like an airplane, it doesn't make sense to 34 00:02:31,860 --> 00:02:36,960 ‫add the self driving functionality to the base class vehicle because not all of our subclasses will 35 00:02:36,960 --> 00:02:37,950 ‫have this feature. 36 00:02:38,580 --> 00:02:44,640 ‫It would be more convenient to add it in the form of an interface called I self driving. 37 00:02:44,760 --> 00:02:50,850 ‫Then these classes that implement this interface can provide the logic to achieve it. 38 00:02:52,050 --> 00:02:55,850 ‫Now let's talk about similarities between interfaces and abstract classes. 39 00:02:55,860 --> 00:02:59,770 ‫Both interfaces and abstract classes can be instantiated. 40 00:02:59,790 --> 00:03:06,390 ‫It means we can't create objects from them, but they both support polymorphism, meaning that we can 41 00:03:06,390 --> 00:03:10,830 ‫store an object of type cat in a variable of type abstract class animal. 42 00:03:11,070 --> 00:03:18,420 ‫The same thing goes for interfaces where we can store any collection like a list or a stack in a variable 43 00:03:18,420 --> 00:03:20,100 ‫of type i enumerable. 44 00:03:21,000 --> 00:03:23,640 ‫So now let's look at the key differences. 45 00:03:23,670 --> 00:03:31,810 ‫An abstract class is designed to be inherited by subclasses that either implement or override its methods. 46 00:03:31,830 --> 00:03:36,900 ‫In other words, abstract classes are either partially implemented or not implemented at all. 47 00:03:37,020 --> 00:03:40,410 ‫On the other hand, interfaces are not implemented at all. 48 00:03:40,950 --> 00:03:45,180 ‫Then an abstract class can't have a constructor opposite to that. 49 00:03:45,180 --> 00:03:51,570 ‫Abstract classes can have constructors and fields as well as method definitions, and the implementation 50 00:03:51,570 --> 00:03:56,070 ‫will be the responsibility of the class that implemented the interface. 51 00:03:57,000 --> 00:04:04,470 ‫On the other hand, an abstract class may contain method, definitions and fields, and since interfaces 52 00:04:04,470 --> 00:04:09,540 ‫don't have any constructors, as we mentioned, they also cannot have fields. 53 00:04:10,320 --> 00:04:16,680 ‫So now classes can implement many interfaces, which is a way to achieve some sort of multi inheritance, 54 00:04:16,680 --> 00:04:20,730 ‫since C-sharp is a single inheritance language and doesn't allow that by default. 55 00:04:20,940 --> 00:04:24,450 ‫So a class can only inherit extent one class. 56 00:04:25,950 --> 00:04:32,430 ‫So if a class implemented an interface, it must implement all of its members, such as properties, 57 00:04:32,430 --> 00:04:34,110 ‫methods and so forth. 58 00:04:34,140 --> 00:04:39,960 ‫On the other hand, when a class extends an abstract class, it must implement its abstract members 59 00:04:39,960 --> 00:04:40,560 ‫only. 60 00:04:40,650 --> 00:04:45,090 ‫So the non implemented part of the abstract class. 61 00:04:45,420 --> 00:04:47,340 ‫Let's look at an analogy here. 62 00:04:47,460 --> 00:04:54,150 ‫Imagine I see sharp instructor at tutorials EU where the instructor can't achieve anything unless he's 63 00:04:54,150 --> 00:05:00,630 ‫an expert in a topic or she's an expert in a topic because we can't have instances of abstract classes. 64 00:05:00,750 --> 00:05:06,060 ‫So an instructor would need to become an expert in a topic to teach it. 65 00:05:06,060 --> 00:05:11,730 ‫So the instructor became a sharp expert, and now he or she can teach C-sharp. 66 00:05:12,270 --> 00:05:15,700 ‫At some point the C shop instructor had to edit his videos. 67 00:05:15,720 --> 00:05:19,290 ‫Although not all instructors need to do the same thing. 68 00:05:19,620 --> 00:05:29,190 ‫So we implemented the interface I video editor for this to work, so not all instructors need to be 69 00:05:29,190 --> 00:05:34,800 ‫able to edit, but the ones that implement this video editor will have to do so. 70 00:05:35,910 --> 00:05:36,330 ‫All right. 71 00:05:36,330 --> 00:05:42,090 ‫One should you choose inheritance over an interface when designing shop class libraries? 72 00:05:42,210 --> 00:05:47,850 ‫Well, use an abstract class if you have some functionality that you want its subclasses to have. 73 00:05:47,880 --> 00:05:52,950 ‫For instance, if you have a set of functions, you want subclasses to have put them in an abstract 74 00:05:52,950 --> 00:05:53,530 ‫class. 75 00:05:53,550 --> 00:06:00,300 ‫The abstract class is used when the derived class shares its core properties and behavior of the abstract 76 00:06:00,300 --> 00:06:03,420 ‫class, the kind of behavior that actually defines the class. 77 00:06:03,690 --> 00:06:08,640 ‫Use an interface if you want a general contract only behavior or functionality. 78 00:06:08,760 --> 00:06:13,260 ‫Interfaces are typically loose compared to abstract classes. 79 00:06:13,290 --> 00:06:19,920 ‫You wouldn't want to use interfaces in a situation where you're constantly writing the same code for 80 00:06:19,920 --> 00:06:21,870 ‫all of the interface methods. 81 00:06:22,290 --> 00:06:26,370 ‫Use an abstract class and define each method once. 82 00:06:26,910 --> 00:06:28,350 ‫So a quick summary. 83 00:06:28,350 --> 00:06:34,650 ‫When we talk about abstract classes, we defined the characteristics of an object type specifying what 84 00:06:34,650 --> 00:06:35,700 ‫an object is. 85 00:06:35,940 --> 00:06:42,090 ‫When we talk about an interface and define capabilities that we promise to provide, we are talking 86 00:06:42,090 --> 00:06:46,170 ‫about establishing a contract about what the object can do. 87 00:06:46,380 --> 00:06:49,860 ‫So what the object is abstract class. 88 00:06:49,860 --> 00:06:51,210 ‫What the object can do. 89 00:06:52,230 --> 00:06:53,250 ‫An interface. 90 00:06:54,090 --> 00:06:54,510 ‫All right. 91 00:06:54,510 --> 00:07:01,750 ‫So I hope you enjoyed and understood this summary and this overview of the two different types of approaches. 92 00:07:01,770 --> 00:07:08,550 ‫And now let's go ahead and use those concepts in different examples throughout the next lectures and 93 00:07:08,550 --> 00:07:09,870 ‫throughout the rest of the course.