1 00:00:00,150 --> 00:00:00,840 Welcome back. 2 00:00:01,110 --> 00:00:06,450 And this video I would like to cover inheritance and inheritance is one of the main concepts of object 3 00:00:06,450 --> 00:00:12,510 oriented programming, and it allows a class to inherit features such as properties and methods from 4 00:00:12,510 --> 00:00:14,250 another class and reduce them. 5 00:00:14,880 --> 00:00:19,890 So the structure is the following you have a subclass and then you have a super class. 6 00:00:20,310 --> 00:00:26,620 Alternatively, you can call the subclass child class or derive class, and the super class can be called 7 00:00:26,630 --> 00:00:28,740 parent class or base class. 8 00:00:29,220 --> 00:00:35,520 Now let's have a look at those in action, so let's create a new class called vehicle. 9 00:00:36,060 --> 00:00:43,440 Now, this class might have some properties and some methods. 10 00:00:44,270 --> 00:00:49,770 OK, and now we can go ahead and take this class and inherit from it. 11 00:00:50,340 --> 00:00:55,440 So this means that we will take its properties and methods and we will make them reusable. 12 00:00:55,890 --> 00:01:02,030 So let's go ahead and create a new class called car, which is of type of vehicle which inherits from 13 00:01:02,340 --> 00:01:02,840 vehicle. 14 00:01:03,540 --> 00:01:09,090 If you do that, you need to make this class open, so vehicle needs to be an open class. 15 00:01:09,120 --> 00:01:11,820 Otherwise, I cannot use inheritance here. 16 00:01:12,660 --> 00:01:20,130 And that is because all classes in Courtland are final by default, which means they are non inheritable 17 00:01:20,130 --> 00:01:20,730 by default. 18 00:01:21,120 --> 00:01:26,880 So you need to use the open keyword to make a class inheritable, and that's what we're doing here. 19 00:01:26,880 --> 00:01:28,470 So we're using open class vehicle. 20 00:01:28,890 --> 00:01:37,560 Now, let's say I want to have another class which inherits from car, so class electric car that should 21 00:01:37,560 --> 00:01:38,850 inherit from car. 22 00:01:39,380 --> 00:01:43,350 Now problem is car is not open, so I would need to make it open as well. 23 00:01:44,400 --> 00:01:48,570 All right, so if you want to make sure that a class cannot be inherited, you can use a sealed key 24 00:01:48,570 --> 00:01:50,660 word, then this would not work. 25 00:01:50,670 --> 00:01:59,370 So you cannot inherit from a sealed class and you can even see that converts your subclass to an object. 26 00:01:59,880 --> 00:02:04,530 And I'm just going to get rid of that because I want to make it open so that we can reuse it. 27 00:02:05,450 --> 00:02:16,920 Now, this year, our vehicle is the superclass or parent class or base class, so you can use either 28 00:02:16,920 --> 00:02:17,670 of those terms. 29 00:02:18,090 --> 00:02:19,080 Each of them is fine. 30 00:02:19,680 --> 00:02:28,620 Now, this year, our car is the subclass of the vehicle class. 31 00:02:29,550 --> 00:02:34,110 And of course, it's also not just a subclass, but also the child or derived class. 32 00:02:36,200 --> 00:02:41,300 OK, so it's just different terms, but whenever you come across those terms, you should know what 33 00:02:41,300 --> 00:02:41,960 they are, right? 34 00:02:42,440 --> 00:02:49,340 And then at the same time, our car is to superclass parent class based class of vehicle. 35 00:02:50,720 --> 00:03:00,260 And of course, in that case, the electric car is the surplus or childless or derived class of the 36 00:03:00,650 --> 00:03:01,310 car class. 37 00:03:03,310 --> 00:03:03,730 OK. 38 00:03:04,970 --> 00:03:05,360 So. 39 00:03:08,510 --> 00:03:15,050 Let's get rid of vehicle now because I want to only work with car and electric car for now. 40 00:03:15,980 --> 00:03:25,070 So let's say we have some properties in here and let's also say that our car has a primary constructor, 41 00:03:25,070 --> 00:03:27,320 so we want to have some more value in here. 42 00:03:27,400 --> 00:03:33,110 Well, name will be a string value brand will also be a string. 43 00:03:33,590 --> 00:03:36,050 So this car has a name and they brand. 44 00:03:36,710 --> 00:03:43,730 And then here, electric car, suddenly we have an error here, so we need to add a constructor parameter 45 00:03:43,730 --> 00:03:44,060 name. 46 00:03:44,930 --> 00:03:48,740 As you can see here, name is now required and something else is missing. 47 00:03:49,340 --> 00:03:51,800 The constructor parameter Brent is missing. 48 00:03:52,310 --> 00:03:53,150 So you can see. 49 00:03:54,270 --> 00:03:56,730 This is how Courtney expects it. 50 00:03:56,880 --> 00:04:03,360 So you need then to use the same parameters that were used for the car, for your electric car as well. 51 00:04:03,540 --> 00:04:08,520 So there's just a constructor that we're using here, the primary constructor. 52 00:04:08,880 --> 00:04:14,190 Now, of course, the electric car can be extended, so you can add more features to it. 53 00:04:14,640 --> 00:04:21,360 It has to have at least two features of the class that it inherits from, but it can extend features 54 00:04:21,360 --> 00:04:24,390 so it can have additional features such as, for example, battery life. 55 00:04:24,390 --> 00:04:31,410 So an electric car would have battery life, which is of type, let's say double. 56 00:04:32,610 --> 00:04:36,690 So that is the base structure when working with constructors. 57 00:04:36,690 --> 00:04:38,400 So with primary constructors. 58 00:04:40,260 --> 00:04:48,600 Now, let's say we have a ranger here in our car, so let's create this class car first here range. 59 00:04:48,960 --> 00:04:55,410 It's going to be of type double and it's going to have a value of 0.0 just as an example here. 60 00:04:56,300 --> 00:05:08,750 Then it has a function in here fun extend range, which will be the amount which will be of type double. 61 00:05:09,530 --> 00:05:11,840 And if the amount is positive. 62 00:05:13,720 --> 00:05:15,100 So great zero. 63 00:05:15,370 --> 00:05:19,120 Then I want to increase the range by the amount. 64 00:05:20,400 --> 00:05:22,090 OK, so if we. 65 00:05:23,170 --> 00:05:29,830 Go to the fuel station and we fuel up our car and the range will be extended, so we extended the range. 66 00:05:31,000 --> 00:05:36,040 And then finally, I want to have another function here fun drive. 67 00:05:36,850 --> 00:05:40,060 So distance should be double. 68 00:05:41,020 --> 00:05:47,860 And I will just print here drove for distance kilometers. 69 00:05:49,920 --> 00:05:59,640 OK, now I have this class car and of course, our electric car inherits from it, so that means that 70 00:05:59,640 --> 00:06:08,460 if we now go ahead and create a fund main, so our main function and in here I create an object off 71 00:06:08,460 --> 00:06:08,880 car. 72 00:06:09,180 --> 00:06:12,960 So far, my car is type car. 73 00:06:13,290 --> 00:06:18,510 Now, of course, I need to add a string, so I'm going to give it the name, for example, A3, which 74 00:06:18,510 --> 00:06:20,250 is of the brand already. 75 00:06:22,330 --> 00:06:29,860 And then I create a electric car, so my car, so I'm going to call it, is going to be an electric 76 00:06:29,860 --> 00:06:30,400 car. 77 00:06:31,390 --> 00:06:38,590 And here it's going to be Tesla S or maybe s model of type Tesla. 78 00:06:39,250 --> 00:06:42,010 And then it of course, needs also to have a battery life. 79 00:06:42,010 --> 00:06:48,130 So I'm going to give it 85.0, which means 85 kilowatt hours. 80 00:06:49,640 --> 00:06:54,830 All right, and the cool thing now is let's go ahead and use my car, don't drive. 81 00:06:57,820 --> 00:07:04,720 And we will see that it needs a distance, so let's say drive for 200 kilometers and here, of course, 82 00:07:04,870 --> 00:07:06,700 it's a double so 200 to two. 83 00:07:07,120 --> 00:07:14,110 And now let's do the same thing with my car that drive and use also two hundred kilometers. 84 00:07:16,440 --> 00:07:22,710 All right, so if we run that now concede it doesn't complain, even though there is no drive method 85 00:07:23,220 --> 00:07:26,790 inside of my electric car, there's actually nothing in mind extra car class. 86 00:07:27,300 --> 00:07:29,250 It's using nothing. 87 00:07:29,760 --> 00:07:33,090 The only thing it does is it inherits from the car class. 88 00:07:34,560 --> 00:07:37,710 So let's run it and test what the result will be. 89 00:07:38,310 --> 00:07:42,840 And we can see that it says drove for 200 kilometers and drove for 200 kilometers. 90 00:07:43,170 --> 00:07:49,380 So in both cases, it drove for 200 kilometers, even though electric car never implemented a function 91 00:07:49,380 --> 00:07:50,040 called drive. 92 00:07:51,030 --> 00:07:51,450 All right. 93 00:07:51,930 --> 00:07:57,180 But it can still use it because it extends the functionality of its parent class, so it has all of 94 00:07:57,180 --> 00:07:58,740 the features of its parent plus. 95 00:07:59,880 --> 00:08:03,990 It was also this feature called Extend, so we can also go ahead. 96 00:08:04,020 --> 00:08:06,150 Maybe we should rename those. 97 00:08:06,150 --> 00:08:10,810 So that's going to be the Tesla S and Audi A3. 98 00:08:11,430 --> 00:08:16,410 And let's reuse those names because I think it's more telling to use those names. 99 00:08:16,800 --> 00:08:24,050 Now you can see that our Tesla can drive for 200, but our Audi can also drive for 200. 100 00:08:24,060 --> 00:08:28,980 Now let's extend our duration or the range of our Teslas. 101 00:08:29,490 --> 00:08:30,690 So Tesla is not. 102 00:08:31,670 --> 00:08:39,799 Concede has extended range does name range brand to try to string all of those, so it has several. 103 00:08:40,770 --> 00:08:47,220 Functions which we have never created, for example, hash code equals and to string. 104 00:08:47,760 --> 00:08:50,220 And these are the ones that are always available. 105 00:08:51,210 --> 00:08:52,410 These are always available. 106 00:08:52,440 --> 00:08:53,700 Why are they always available? 107 00:08:53,700 --> 00:08:57,090 Because any class inherits from the any. 108 00:08:58,010 --> 00:09:00,320 Class, so I say it again. 109 00:09:00,770 --> 00:09:05,870 Any class inherits from the any class. 110 00:09:06,960 --> 00:09:14,660 So there's a class called any, and we can use this type any as we've seen before. 111 00:09:14,670 --> 00:09:18,450 So for example, I can create an object bar object. 112 00:09:19,290 --> 00:09:21,490 One is of type any. 113 00:09:22,710 --> 00:09:25,020 Okay, so and he's a class in Scotland. 114 00:09:26,070 --> 00:09:26,640 Let's see it. 115 00:09:27,360 --> 00:09:31,250 It's an open class, the route of Scotland class hierarchy. 116 00:09:31,260 --> 00:09:33,090 Every Scotland class has any. 117 00:09:34,390 --> 00:09:35,380 As a superclass. 118 00:09:36,850 --> 00:09:44,110 And that's why we can always use the function or the method equals hash code and to string. 119 00:09:45,680 --> 00:09:48,950 And can overwrite them as well, we can create our own types of those. 120 00:09:49,340 --> 00:09:52,940 But now let's go back and use the extend range here. 121 00:09:53,090 --> 00:09:56,000 So let's extend the range by 200. 122 00:09:57,310 --> 00:09:58,840 So now we extended. 123 00:09:59,820 --> 00:10:04,200 The potential distance that our Tesla last can drive by 200 kilometers. 124 00:10:05,740 --> 00:10:09,970 And that is because we have this fun extend range in our car class. 125 00:10:10,390 --> 00:10:18,070 Now let's go ahead and let's say we want to override the range functionality and we want to override 126 00:10:18,490 --> 00:10:20,890 the drive functionality. 127 00:10:21,220 --> 00:10:30,310 So what we can do in our class electric car, we can just go ahead and say override bar range is battery 128 00:10:30,640 --> 00:10:32,320 life multiplied with six? 129 00:10:32,830 --> 00:10:42,550 So let's say with 85 watt hours and we can drive something around five hundred and ten kilometers or 130 00:10:42,550 --> 00:10:44,200 so with this range. 131 00:10:44,740 --> 00:10:46,300 So it's more or less realistic. 132 00:10:46,300 --> 00:10:51,670 I think maybe it's just five, but something around that five or six times the battery life in kilowatt 133 00:10:51,670 --> 00:10:53,860 hours is what a Tesla can drive. 134 00:10:54,580 --> 00:10:59,470 So now we are overriding the range property. 135 00:10:59,830 --> 00:11:01,240 Now, of course, it's complaining. 136 00:11:01,240 --> 00:11:08,170 It says, OK, you cannot override range because the range is not open, so you can make. 137 00:11:09,340 --> 00:11:10,990 A property open as well. 138 00:11:11,200 --> 00:11:14,770 So here we can use open for this property called the ranch. 139 00:11:15,550 --> 00:11:19,360 We can use the same feature for a function. 140 00:11:19,480 --> 00:11:22,630 So now we made this drive function open as well. 141 00:11:22,630 --> 00:11:29,920 So now we can go ahead and overwrite that functionality so we can say, for example, in our electric 142 00:11:29,920 --> 00:11:31,480 car, that we. 143 00:11:33,340 --> 00:11:45,640 Drove his override, fun drive, and we say we drove a certain distance, but we also print line. 144 00:11:46,120 --> 00:11:52,420 Say something like drove distance on electricity, but that by itself wouldn't be as interesting as 145 00:11:52,420 --> 00:11:55,960 doing the following so we can confuse the whole thing. 146 00:11:56,080 --> 00:11:58,000 Let's go ahead and say drive. 147 00:11:59,860 --> 00:12:08,050 And now we just print something like drove range kilometers on electricity. 148 00:12:09,340 --> 00:12:17,020 So now what we have is we have a drive function, which we override from our parent class and we have 149 00:12:17,020 --> 00:12:21,760 a drive function, which we created inside of our inheritance. 150 00:12:26,510 --> 00:12:30,250 Which we created inside of our subclass. 151 00:12:31,460 --> 00:12:38,030 This might not always be a good idea, because it might end up being confusing, but in the end, now 152 00:12:38,030 --> 00:12:40,910 you can go ahead and say Tesla start drive. 153 00:12:41,390 --> 00:12:46,910 And you can see you have two options here, so you can either drive or you can drive a distance. 154 00:12:47,420 --> 00:12:49,730 So let's say we drive. 155 00:12:50,240 --> 00:12:55,850 So before we used to distance feature and now we just drive because we want to know how far a Tesla 156 00:12:55,850 --> 00:13:03,710 can drive and if we run that again, then we should be able to see the maximum range of our Tesla S. 157 00:13:06,530 --> 00:13:11,300 And we can see that we get 710 kilometers on electricity here. 158 00:13:11,930 --> 00:13:21,650 So we have created an overriding function, so we are overriding the functionality of our superclass, 159 00:13:22,610 --> 00:13:24,620 so it would change whatever was written in here. 160 00:13:25,070 --> 00:13:31,910 And then we create another function, which is now specific to our electric car, which has the same 161 00:13:31,910 --> 00:13:33,830 name, but it uses different features. 162 00:13:35,130 --> 00:13:41,070 Of course, we can have a bunch of other variables in here as well, so we cannot just have the variables 163 00:13:41,070 --> 00:13:42,750 that are used in our. 164 00:13:43,710 --> 00:13:50,130 Superclass here for the electric car, but we can also have a charger type, for example, let's say 165 00:13:51,210 --> 00:13:55,740 different electric cars have different charger types or something like that than we can say, Okay, 166 00:13:55,740 --> 00:13:58,200 this car has a charger type of one. 167 00:13:58,710 --> 00:14:04,740 And if you want to change dad, we can of course do so for every single Tesla s that. 168 00:14:05,770 --> 00:14:09,910 Charger type, and let's say it has a type two charger. 169 00:14:10,510 --> 00:14:15,070 All right, and there's one thing that I should say that what we're doing here with these two. 170 00:14:16,600 --> 00:14:19,240 So let me put that a little further down. 171 00:14:19,780 --> 00:14:26,770 What we're doing here is called poly more, because at least that's part of polymorphism. 172 00:14:27,190 --> 00:14:33,430 Polymorphism is the ability to treat objects with similar traits in a common way, which we are doing 173 00:14:33,430 --> 00:14:40,510 right here because a Tesla has common traits, because it's inheriting or it's of type electric car 174 00:14:40,510 --> 00:14:42,100 and it's inheriting from car. 175 00:14:43,660 --> 00:14:50,380 And this, of course, only works because electric car is a subclass of car that would alternatively 176 00:14:50,380 --> 00:14:55,870 also work if it was a subclass of car, which was an interface. 177 00:14:56,230 --> 00:14:59,020 OK, so if car was an interface, that would work as well. 178 00:14:59,290 --> 00:15:03,880 But what interfaces are is something that we're going to look at in another lecture. 179 00:15:05,530 --> 00:15:12,250 All right, so that was a little introduction into inheritance, and let's see some more examples of 180 00:15:12,250 --> 00:15:16,300 that, for example, with interfaces and the next video.