1 00:00:00,910 --> 00:00:06,130 Inheritance is one of those fundamental aspects of object oriented programming. 2 00:00:06,180 --> 00:00:11,980 It means classes can inherit properties or functions from superclasses. 3 00:00:12,000 --> 00:00:15,960 It means one class can become a base class for somebody else. 4 00:00:15,960 --> 00:00:20,970 Now the good news is you have actually been using inheritance for the most part of everything we have 5 00:00:20,970 --> 00:00:21,870 been doing. 6 00:00:21,870 --> 00:00:29,220 But now in this lesson I'm actually going to make my own classes and then have a parent child relationship 7 00:00:29,220 --> 00:00:30,170 between them. 8 00:00:30,390 --> 00:00:31,540 That's how we can do that. 9 00:00:31,590 --> 00:00:40,910 I'm going to go to school and start a new project and I'm going to call it inheritance science street. 10 00:00:41,550 --> 00:00:46,890 So here is how what I want to do and how I'm going to do it I'm going to write it down first I'm going 11 00:00:46,890 --> 00:00:54,720 to have a class of vehicles and then my class because I'm assuming this the vehicles that they go on 12 00:00:54,720 --> 00:00:56,380 land essentially. 13 00:00:56,520 --> 00:01:02,850 Now I'm going to say that has a subclass called sedans. 14 00:01:02,850 --> 00:01:09,000 All right so I have the Waco's class and from there we have the seeding classes and then begin to learn 15 00:01:09,000 --> 00:01:11,510 how to override functions. 16 00:01:11,700 --> 00:01:19,590 And then we're going to learn how to add new functions and all that's pretty much most of for the really 17 00:01:19,590 --> 00:01:20,250 matters. 18 00:01:20,250 --> 00:01:22,020 So let's go ahead and get started. 19 00:01:22,020 --> 00:01:24,810 I have a class called Veeck. 20 00:01:25,130 --> 00:01:27,630 And it doesn't have a superclass. 21 00:01:27,630 --> 00:01:34,390 What it does have is this it has a variable called tires and that's an integer. 22 00:01:34,510 --> 00:01:38,500 Explicitly wrapped optional that it must be there. 23 00:01:38,520 --> 00:01:41,730 So my vehicle has a number of tires. 24 00:01:41,730 --> 00:01:43,420 How many tires are there. 25 00:01:43,560 --> 00:01:51,300 And then I'm going to go ahead and say it has an in it in tires and there's going to be an integer because 26 00:01:51,300 --> 00:01:56,670 you can imagine make self tires be calm in types. 27 00:01:56,700 --> 00:02:01,890 I'm actually going to go ahead and add one more function of my own as that I'm going to call it. 28 00:02:01,940 --> 00:02:04,400 Charlie vate. 29 00:02:04,580 --> 00:02:08,690 Let's call it an integer input rate. 30 00:02:08,700 --> 00:02:17,670 So essentially of the other reference I am writing in in an X and Objective-C so input rate which is 31 00:02:17,670 --> 00:02:19,330 going to be an integer. 32 00:02:19,440 --> 00:02:19,980 Right. 33 00:02:20,310 --> 00:02:23,880 So I have that and the return is actually a boolean. 34 00:02:23,880 --> 00:02:30,240 So whether this is going to be possible or not and I'm going to say by default the carry rate actually 35 00:02:30,240 --> 00:02:31,940 returns false. 36 00:02:32,040 --> 00:02:38,760 Unless our vehicle can carry something so let's see how we're going to subclassed these and how are 37 00:02:38,760 --> 00:02:41,270 we going to inherit this stuff from it. 38 00:02:41,280 --> 00:02:45,420 Let me clear things up so we can actually you know write more code in here. 39 00:02:45,690 --> 00:02:54,370 So the next class I have is a class of sedans so I'm going to say class now when I'm defining my sedan 40 00:02:54,420 --> 00:02:58,760 class I'm telling myself well every seat on is kind of a vehicle. 41 00:02:58,830 --> 00:03:04,260 It has to have a certain number of tires and it can carry a certain amount of weight. 42 00:03:04,270 --> 00:03:09,270 It might have an interest in it that initializes a certain number of tires. 43 00:03:09,270 --> 00:03:17,820 So what if I say my sedan is actually a child of vehicles or you could read it in a way that CNN has 44 00:03:17,820 --> 00:03:22,900 a base class called vehicle or vehicle is the superclass of Sedan. 45 00:03:23,070 --> 00:03:24,600 So that's about that. 46 00:03:24,630 --> 00:03:29,670 I have this now and I have a class called sedan and if I wanted to I could actually go ahead and use 47 00:03:29,670 --> 00:03:29,990 it. 48 00:03:30,180 --> 00:03:34,420 Let me again get myself a little bit more space and I'm going to use it for you. 49 00:03:34,680 --> 00:03:38,510 I'm going to say let me bring this one down. 50 00:03:38,820 --> 00:03:40,400 OK. 51 00:03:40,470 --> 00:03:48,600 All right so now I'm going to go ahead and say let's call it what's a C Now let's say Ford Fusion for 52 00:03:48,600 --> 00:03:49,910 the fusion. 53 00:03:49,920 --> 00:03:50,610 That's a sin. 54 00:03:50,640 --> 00:03:52,080 I hope so. 55 00:03:52,080 --> 00:03:53,150 It's a sedan. 56 00:03:53,340 --> 00:04:00,230 And the only type of it that it has is the input of number of tires they say well it has four tires. 57 00:04:00,450 --> 00:04:05,880 So now my sedan class is essentially the same class as vehicle. 58 00:04:05,880 --> 00:04:07,780 It's exactly the same thing. 59 00:04:08,040 --> 00:04:15,910 But what have see now I have a variable called sunroof and that would be a boolean value. 60 00:04:16,170 --> 00:04:20,630 And that's the specific to see whether they have sun roof or not. 61 00:04:20,630 --> 00:04:28,000 And now as you can imagine in Ford Fusion I could go ahead and say some truth is for instance true. 62 00:04:28,230 --> 00:04:33,370 This same wasn't valid for something that is made of the type we cause. 63 00:04:33,580 --> 00:04:41,220 In theory I could go ahead in here and say my Ford Fusion is a vehicle with input tires for. 64 00:04:41,220 --> 00:04:46,130 But then this one is going to give us an Aero is going to say so what is sunroof in here exactly. 65 00:04:46,140 --> 00:04:49,680 Because a vehicle doesn't have a memory card sort of. 66 00:04:49,780 --> 00:04:57,530 So so far we managed to have a class that borrows some of stuff from its parents class C Don borrows 67 00:04:57,540 --> 00:05:02,320 from bacon and adds its own set of parameters and attributes. 68 00:05:02,370 --> 00:05:05,340 Siddhant and also have its own in it. 69 00:05:05,340 --> 00:05:11,160 So for instance I'm going to see on every seat on has a number of tires which is four. 70 00:05:11,370 --> 00:05:22,170 So I'm going to go ahead and say it him Sundra of sun roof which is a boolean value and then you say 71 00:05:22,170 --> 00:05:27,170 the body of my knee I'm gonna say the first thing is I have to initialize this. 72 00:05:27,180 --> 00:05:29,640 Based on its superclass. 73 00:05:29,720 --> 00:05:35,500 So I'm going to say super dots in it and say well what's the number of the tires. 74 00:05:35,500 --> 00:05:38,060 And let's say the number of those is always four. 75 00:05:38,250 --> 00:05:45,700 So whenever I see is being shallows that's going to be for and then I'm going to say self son who is 76 00:05:45,700 --> 00:05:48,700 going to be in put some of. 77 00:05:48,700 --> 00:05:57,250 So now we have a new unit for RCA Don that does initialize or object based on a sound off and we can 78 00:05:57,250 --> 00:05:58,270 use that. 79 00:05:58,300 --> 00:06:02,430 I'm going to go in here and say sit down in put some growth through. 80 00:06:02,740 --> 00:06:04,630 Well I don't need that line anymore. 81 00:06:04,840 --> 00:06:06,250 So what happened so far. 82 00:06:06,250 --> 00:06:10,260 We have a base class the base class has its own initialiser. 83 00:06:10,420 --> 00:06:17,890 It has a variable in there and we have a child's class or a sub class and subclass has a new property 84 00:06:17,890 --> 00:06:19,310 or a new attribute. 85 00:06:19,420 --> 00:06:21,850 I has a new in it. 86 00:06:22,030 --> 00:06:28,810 Now here's another thing a very common situation that happens sometimes you want to overwrite an existing 87 00:06:28,870 --> 00:06:29,380 in it. 88 00:06:29,530 --> 00:06:31,660 So how do you do that. 89 00:06:32,170 --> 00:06:39,580 I'm you're on say override in it and it says well you're trying to override it. 90 00:06:39,670 --> 00:06:40,230 This one. 91 00:06:40,240 --> 00:06:41,830 Yes that's the one I'm going to do. 92 00:06:41,890 --> 00:06:42,590 And where are you going to go. 93 00:06:42,610 --> 00:06:47,650 I'm going to say super DOD in it put the same value in them. 94 00:06:47,770 --> 00:06:48,160 All right. 95 00:06:48,160 --> 00:06:53,600 Or if you want manually pull put forward which kind of renders this one moot. 96 00:06:53,680 --> 00:07:02,410 Now the next thing I'm going to do is I'm going to say Songdo is Fox so by default whenever I override 97 00:07:02,590 --> 00:07:09,570 this in it and when I get the sunroof to be favs and that's overwriting and initialiser from our superclass. 98 00:07:09,730 --> 00:07:16,220 Imagine if you're initialiser you have to do something different than you or our parents class and that's 99 00:07:16,220 --> 00:07:21,330 something you override the initialiser you could of course overwrite an existing function. 100 00:07:21,340 --> 00:07:27,010 I could go in here and say as you can imagine there is no order between the methods of just writing 101 00:07:27,030 --> 00:07:33,700 in here so it's easier to zoom in to the screen and what to say overwrite function and the function 102 00:07:33,760 --> 00:07:36,220 I want to override is this guy. 103 00:07:36,320 --> 00:07:37,960 So not in this. 104 00:07:37,960 --> 00:07:38,550 All right. 105 00:07:38,560 --> 00:07:50,300 I'm going to say well it doesn't always return false if a simple rate is below 2000 than our return 106 00:07:50,790 --> 00:07:55,850 through else let me put the else over there as well. 107 00:07:55,880 --> 00:08:02,210 I'm trying to be more economical with my space illustrator and false of course. 108 00:08:02,210 --> 00:08:05,990 So that's about you know overriding a function. 109 00:08:05,990 --> 00:08:10,340 So that's pretty much the basics of inheritance. 110 00:08:10,340 --> 00:08:17,340 You have a class that class has some stuff in it you subclass it to another one and that one can add 111 00:08:17,420 --> 00:08:25,250 on a stuff overwrite functions from parent class override units from power Pyne's class have its own 112 00:08:25,300 --> 00:08:30,810 specific in it such as this one and work essentially independently. 113 00:08:30,890 --> 00:08:35,600 Something you might be wondering is so one why would we use this. 114 00:08:35,670 --> 00:08:37,690 When do we use a situation like this. 115 00:08:37,740 --> 00:08:39,540 Almost entirely always. 116 00:08:39,560 --> 00:08:45,620 And that's the principle of object oriented programming trying to modularize your code. 117 00:08:45,740 --> 00:08:48,880 So it is essentially easier to write and to read. 118 00:08:49,010 --> 00:08:54,490 So if I'm writing an application let's forget about the cause and see what happens and what not. 119 00:08:54,620 --> 00:09:00,700 Let's imagine I'm writing an application that say real estate listing application. 120 00:09:00,710 --> 00:09:03,890 You have multiple different types of states right. 121 00:09:03,890 --> 00:09:10,250 So for instance you have listings that are for sale you have listed that are for rental and then you 122 00:09:10,250 --> 00:09:15,690 have listening that are residential and then you have these things that are commercial and then you 123 00:09:15,690 --> 00:09:19,550 have less things that are in the city. 124 00:09:19,560 --> 00:09:20,160 All right. 125 00:09:20,300 --> 00:09:26,090 And then you have these things that are perhaps you know out of a state or whatever that you are trying 126 00:09:26,090 --> 00:09:26,850 to do. 127 00:09:26,890 --> 00:09:31,100 There are lot of different things that are common between all of these things. 128 00:09:31,220 --> 00:09:38,270 So in a situation like this I would go ahead and say class property and then subclass sale and then 129 00:09:38,270 --> 00:09:41,670 from subclass of state I would get something called residential. 130 00:09:41,820 --> 00:09:47,960 Let's quickly demonstrate say that I'm going to say costis call it property and this is a class that 131 00:09:47,960 --> 00:09:54,680 has the building blocks of a listing that you know you can have your app and then on that I have a class 132 00:09:54,740 --> 00:09:57,290 and that class is going to be sale. 133 00:09:57,590 --> 00:10:02,380 And that's a subclass of property property. 134 00:10:02,840 --> 00:10:10,640 And then I'm going to have another class that's going to be residential and that superclass of that 135 00:10:10,640 --> 00:10:11,840 is going to be saved. 136 00:10:11,960 --> 00:10:17,710 So it's essentially a hierarchy of different objects that they inherit from each other. 137 00:10:17,750 --> 00:10:19,160 Every one is a property. 138 00:10:19,250 --> 00:10:20,780 Everyone is for sale. 139 00:10:20,780 --> 00:10:25,140 So let's imagine everyone has something called price. 140 00:10:25,220 --> 00:10:25,580 Right. 141 00:10:25,580 --> 00:10:29,150 So price is an integer value for instance. 142 00:10:29,150 --> 00:10:30,660 So everybody has a price. 143 00:10:30,830 --> 00:10:33,170 And then the ones that are for sale. 144 00:10:33,350 --> 00:10:36,580 They also have availability for instance. 145 00:10:36,580 --> 00:10:42,270 So let's go ahead and say Vire Let's see. 146 00:10:42,320 --> 00:10:45,730 And that's going to be a date right. 147 00:10:45,800 --> 00:10:52,640 And then the ones that are residential are for instance they have something called variable number of 148 00:10:52,690 --> 00:10:54,170 or just rooms. 149 00:10:54,280 --> 00:11:02,030 All right which is a number of the rooms that are available you know residential for sale property so 150 00:11:02,030 --> 00:11:06,050 on and so forth you know all of these things they essentially trickle down. 151 00:11:06,050 --> 00:11:11,990 They go from one to another to another and that's you that's how your code becomes more and more modularized 152 00:11:12,020 --> 00:11:14,630 and easier to read and easier to understand. 153 00:11:14,840 --> 00:11:20,390 We will see a lot of these kind of things in the coming you know examples on the actual apps that we 154 00:11:20,390 --> 00:11:21,040 make. 155 00:11:21,050 --> 00:11:23,660 We try to subclass as much as we can. 156 00:11:23,660 --> 00:11:29,420 Another thing that you know you often end up doing is subclassing something that already exists. 157 00:11:29,570 --> 00:11:39,680 Phrase as I want to get a class called my a string to be a subclass of the string because I want to 158 00:11:39,680 --> 00:11:41,920 add an extra function to it. 159 00:11:41,930 --> 00:11:47,120 I want something to happen in here which Genesys thing doesn't do it by default. 160 00:11:47,210 --> 00:11:53,510 That's all I'm working on an app that has to I don't know who to to reverse every listing that I give 161 00:11:53,510 --> 00:11:53,890 it to. 162 00:11:53,890 --> 00:11:58,910 I give to him and I want to have the built in feature of this thing so I can just going here on a new 163 00:11:58,910 --> 00:12:04,980 class that inherits everything from a string but also has some extra stuff in it. 164 00:12:05,200 --> 00:12:11,630 And we will see more of this when we talk about class categories and class methods as well. 165 00:12:11,630 --> 00:12:20,150 For now if you have a class this is how you stop classes this is how you inherit from that one you are 166 00:12:20,180 --> 00:12:24,860 good on the stuff you overwrite functions and you use them independently. 167 00:12:24,860 --> 00:12:26,210 That's about that. 168 00:12:26,210 --> 00:12:27,500 And let's move on to our next guest.