1 00:00:08,030 --> 00:00:13,050 Welcome back. Sometimes, when creating an inherited class, 2 00:00:13,050 --> 00:00:16,785 you want to create a method that calls the super classes method, 3 00:00:16,785 --> 00:00:18,570 but just does a little bit extra. 4 00:00:18,570 --> 00:00:20,250 Now let me explain an example. 5 00:00:20,250 --> 00:00:26,460 So, let's suppose that I have this pet class and inside of the pet class, 6 00:00:26,460 --> 00:00:29,875 I have a method, feed. 7 00:00:29,875 --> 00:00:34,640 Now suppose that I wanted to create something that inherited from the pet class. 8 00:00:34,640 --> 00:00:37,010 Let's suppose that I wanted to create a class dog. 9 00:00:37,010 --> 00:00:41,785 So I'm just going to draw out the beginnings of that class dog, 10 00:00:41,785 --> 00:00:43,915 and I want it to inherit from pet, 11 00:00:43,915 --> 00:00:46,580 so I'll say pet in parentheses. 12 00:00:46,580 --> 00:00:50,600 Now, let's suppose that whenever I fed my dog, 13 00:00:50,600 --> 00:00:54,355 I wanted it to both called this feed method, 14 00:00:54,355 --> 00:00:58,700 but then let's suppose that every dog instance also has a little extra message. 15 00:00:58,700 --> 00:01:00,605 So, let's suppose that a dog says Arf! 16 00:01:00,605 --> 00:01:03,620 Thanks, whenever pet is called on that dog. 17 00:01:03,620 --> 00:01:11,070 So, if I did feed itself, 18 00:01:11,630 --> 00:01:22,260 if I did this and then let's say that I print out Arf! 19 00:01:22,260 --> 00:01:27,420 Thanks! 20 00:01:27,420 --> 00:01:30,740 So, if I define my dog class this way, 21 00:01:30,740 --> 00:01:32,990 whenever I create a new instance of dog, 22 00:01:32,990 --> 00:01:35,360 and then call feed on that dog, 23 00:01:35,360 --> 00:01:37,390 then all that happens is Arf! 24 00:01:37,390 --> 00:01:38,870 Thanks gets printed out. 25 00:01:38,870 --> 00:01:41,630 Because when I define this feed method, 26 00:01:41,630 --> 00:01:45,515 I am overwriting the feed method in the parent class. 27 00:01:45,515 --> 00:01:48,380 So, this would never get called, 28 00:01:48,380 --> 00:01:50,570 at least not by default, 29 00:01:50,570 --> 00:01:52,625 but there is a way to call it. 30 00:01:52,625 --> 00:01:56,445 So, the way they call the feed method for 31 00:01:56,445 --> 00:01:58,955 the pet class in addition to 32 00:01:58,955 --> 00:02:02,120 printing out this little extra bit of code that I wanted to run. 33 00:02:02,120 --> 00:02:03,800 The way that I would do that is, 34 00:02:03,800 --> 00:02:06,970 I would say pet, capital P, pet. 35 00:02:06,970 --> 00:02:08,910 So, the name of the class, 36 00:02:08,910 --> 00:02:11,620 and then I would say.feed, 37 00:02:13,070 --> 00:02:17,230 and I would call this, on self. 38 00:02:20,150 --> 00:02:24,965 Now, notice that this is calling feed on the pet class. 39 00:02:24,965 --> 00:02:26,570 Normally, when we call feed, 40 00:02:26,570 --> 00:02:27,890 we create a new instance, 41 00:02:27,890 --> 00:02:32,250 so I might say D1 equals new dog, 42 00:02:36,410 --> 00:02:42,720 and normally I would say d1.feed, 43 00:02:42,720 --> 00:02:47,060 then I would pass in no arguments here because D1 is an instance. 44 00:02:47,060 --> 00:02:50,730 Here instead, I'm saying the name of the class directly, 45 00:02:50,730 --> 00:02:52,890 so I'm saying pet.feed, 46 00:02:52,890 --> 00:03:01,580 and since I haven't specified which pet instance I'm calling feed on, 47 00:03:01,580 --> 00:03:07,160 then I need to pass in that particular instance as the first argument to pet dog. 48 00:03:07,160 --> 00:03:11,040 If I tried to say something like self.feed, 49 00:03:14,690 --> 00:03:21,315 here then self.feed is just going refer to the dogs' feed method. 50 00:03:21,315 --> 00:03:26,015 So, again, the way that I'll create a feed method on a subclass, 51 00:03:26,015 --> 00:03:29,450 that has everything that the superclass feed method has, 52 00:03:29,450 --> 00:03:31,430 and more I'll cloud call 53 00:03:31,430 --> 00:03:36,020 the superclass feed method by saying the name of the superclass in this case pet, 54 00:03:36,020 --> 00:03:38,570 and then I'll say whatever method I want to call, 55 00:03:38,570 --> 00:03:41,345 and then I'll pass in that instance directly. 56 00:03:41,345 --> 00:03:45,215 So, in code this is what that might look like. 57 00:03:45,215 --> 00:03:48,170 So, here I have a dog class, 58 00:03:48,170 --> 00:03:50,900 and dog class inherits from pet, 59 00:03:50,900 --> 00:03:53,105 but it has its own feed method. 60 00:03:53,105 --> 00:03:55,175 Inside of that feed method, 61 00:03:55,175 --> 00:03:57,635 I first call pet dog feed, 62 00:03:57,635 --> 00:04:01,040 with whatever instance this feed method is being called on, 63 00:04:01,040 --> 00:04:05,160 and then I print out our thanks after the pet. 64 00:04:05,160 --> 00:04:07,055 dog feed method has been called. 65 00:04:07,055 --> 00:04:09,680 Now this also works for constructors. 66 00:04:09,680 --> 00:04:15,240 So, let's suppose that I have another subclass of pet called, bird. 67 00:04:15,700 --> 00:04:21,575 Let's suppose that the bird class has it's own constructor, 68 00:04:21,575 --> 00:04:25,790 but I want to call the pet classes constructor as well. 69 00:04:25,790 --> 00:04:27,215 Well, the way that I would do that, 70 00:04:27,215 --> 00:04:29,900 is I would say pet._init, 71 00:04:29,900 --> 00:04:32,960 and I would call it with whatever arguments I want to call. 72 00:04:32,960 --> 00:04:34,895 So, in this case, I'll pass in self, 73 00:04:34,895 --> 00:04:37,255 and then I'll pass in the name. 74 00:04:37,255 --> 00:04:41,570 What this is going to do is it's going to call the pet classes constructor, 75 00:04:41,570 --> 00:04:43,865 on this new instance bird. 76 00:04:43,865 --> 00:04:45,455 So, it's going to set name, 77 00:04:45,455 --> 00:04:48,410 hunger, boredom, and sounds. 78 00:04:48,410 --> 00:04:51,760 So, let's do some multiple choice questions. 79 00:04:51,760 --> 00:04:53,655 Okay. So, this question asks, 80 00:04:53,655 --> 00:04:57,930 what would print one print out b1.sounds is run? 81 00:04:57,930 --> 00:05:01,575 So, B1, here is an instance of bird, 82 00:05:01,575 --> 00:05:04,110 and if I'm calling b1.sounds, 83 00:05:04,110 --> 00:05:07,140 then you'll see that's instance variable, 84 00:05:07,140 --> 00:05:10,065 that gets set right here. 85 00:05:10,065 --> 00:05:16,765 So, here we're setting self.sound equals to a copy of self.sounds. 86 00:05:16,765 --> 00:05:23,360 By default every pet has sounds list of [inaudible]. 87 00:05:23,360 --> 00:05:30,670 You'll see that the bird class has a class variable sounds set to chirp. 88 00:05:30,670 --> 00:05:34,380 So, when I call pet._init, 89 00:05:35,480 --> 00:05:42,145 when it said self.sounds equal the self.sounds is copy self.sounds, 90 00:05:42,145 --> 00:05:46,040 what that's going to do because B1 is a bird, 91 00:05:46,040 --> 00:05:51,650 is it's going to create a copy of this list of sounds, 92 00:05:51,650 --> 00:05:56,580 and so b1.sounds ends up being chirp. 93 00:05:56,580 --> 00:06:01,085 So again, the reason that it's creating a copy of this list of sounds, 94 00:06:01,085 --> 00:06:07,140 is because we have the superclass pet, 95 00:06:15,970 --> 00:06:20,090 then we have a subclass bird. 96 00:06:20,090 --> 00:06:23,430 So, every bird is a pet, 97 00:06:26,900 --> 00:06:31,110 and then we have an instance of bird, 98 00:06:32,620 --> 00:06:36,185 and that instance is called B1. 99 00:06:36,185 --> 00:06:40,470 Bird has its own value per sounds, 100 00:06:45,460 --> 00:06:50,390 and pet has its own different value for sounds. 101 00:06:56,220 --> 00:06:59,400 Now when we say b1.sounds, 102 00:06:59,400 --> 00:07:04,570 or self.sounds in the constructor equal a copy of self.sounds, 103 00:07:04,570 --> 00:07:09,665 then Python is going to first look for what is the value of self.sounds. 104 00:07:09,665 --> 00:07:12,190 So, let's go back to the constructor. 105 00:07:12,190 --> 00:07:14,260 So when said self.sounds, 106 00:07:14,260 --> 00:07:18,050 here again self is B1. 107 00:07:18,750 --> 00:07:25,735 So, we're setting b1.sounds equal to b1.sounds, 108 00:07:25,735 --> 00:07:29,780 and we're slicing it so that we create a copy of b1.sounds. 109 00:07:29,780 --> 00:07:32,195 When Python is searching for b1.sounds, 110 00:07:32,195 --> 00:07:35,180 here it first searches inside of the instance. 111 00:07:35,180 --> 00:07:38,840 This instance does it have sounds instance variable yet, 112 00:07:38,840 --> 00:07:42,410 and then it searches inside of that instances class, 113 00:07:42,410 --> 00:07:45,320 and so we find chirp, 114 00:07:45,320 --> 00:07:49,450 or a list that only contains chirp as the list of sounds. 115 00:07:49,450 --> 00:07:53,190 So, here we end up creating a copy of that. 116 00:07:59,750 --> 00:08:02,400 So, I mentioned all of that, 117 00:08:02,400 --> 00:08:04,545 just to say that the answer to this, 118 00:08:04,545 --> 00:08:11,940 is C. Let's do another question. 119 00:08:11,940 --> 00:08:15,695 This question asks, for the dog class defined in the earlier window, 120 00:08:15,695 --> 00:08:19,355 what would happen when d1.feed is run, 121 00:08:19,355 --> 00:08:23,330 if the pet.feed self line was deleted? 122 00:08:23,330 --> 00:08:28,560 So, in other words, if we deleted, 123 00:08:28,560 --> 00:08:30,760 or commented out this line, 124 00:08:30,760 --> 00:08:33,920 then what would happen when we called D1, 125 00:08:33,920 --> 00:08:35,735 which is a new instance of dog, 126 00:08:35,735 --> 00:08:38,940 what would happen, if we call d1.feed? 127 00:08:38,940 --> 00:08:44,280 So, if this pet.feet(self) line was deleted, 128 00:08:44,280 --> 00:08:49,320 then the only thing in the dog feed method that actually runs, 129 00:08:49,320 --> 00:08:51,530 is this print statement prints Arf! 130 00:08:51,530 --> 00:08:56,180 Thanks! So, remember that the pet feed method is never called by default, 131 00:08:56,180 --> 00:08:59,510 because this method overrides that method entirely. 132 00:08:59,510 --> 00:09:02,600 So, the only thing that would happen is print Arf! 133 00:09:02,600 --> 00:09:04,585 Thanks! would run. 134 00:09:04,585 --> 00:09:07,050 So, that means that the answer here, 135 00:09:07,050 --> 00:09:15,160 is C. That's all for now, until next time