1 00:00:07,880 --> 00:00:11,790 Welcome back. So, we've already covered inheritance, 2 00:00:11,790 --> 00:00:14,490 but it's worth talking about when we actually want to 3 00:00:14,490 --> 00:00:17,445 inherit and how we want to inherit at times. 4 00:00:17,445 --> 00:00:20,430 So, in general, the way that Python uses 5 00:00:20,430 --> 00:00:24,390 inheritance is that let's suppose that we have a superclass. 6 00:00:24,390 --> 00:00:28,725 So, I'm going to represent classes by this factory. 7 00:00:28,725 --> 00:00:31,290 I think that factory is a good metaphor for classes, 8 00:00:31,290 --> 00:00:33,855 a factory for constructing instances. 9 00:00:33,855 --> 00:00:38,980 So, let's say that this red factory represents a superclass. 10 00:00:42,740 --> 00:00:47,000 The superclass is the class that we're inheriting from. 11 00:00:47,000 --> 00:00:57,450 Let's suppose that this blue factory represents a subclass. 12 00:00:58,910 --> 00:01:02,680 So, the subclass inherits from the superclass. 13 00:01:02,680 --> 00:01:06,200 I'm going to represent that just by having this little red dot here, 14 00:01:06,200 --> 00:01:09,620 and that's like a line representing that we remember 15 00:01:09,620 --> 00:01:13,550 that this subclass is inherited from that superclass. 16 00:01:13,550 --> 00:01:16,540 Then, we of course have instances. 17 00:01:16,540 --> 00:01:19,830 So, you might have an instance. 18 00:01:19,830 --> 00:01:24,490 Let's suppose that this instances of the subclass. 19 00:01:25,480 --> 00:01:29,720 I have this blue dot here to represent that Python remembers 20 00:01:29,720 --> 00:01:33,785 that the instance is an instance of the subclass. 21 00:01:33,785 --> 00:01:39,050 Now, whenever Python is looking for any method or instance variables, 22 00:01:39,050 --> 00:01:42,540 it's always first going to look in the instance. 23 00:01:42,700 --> 00:01:47,175 So, it's first going to look inside of the instance, 24 00:01:47,175 --> 00:01:50,540 and it's going to ask does the instance have that thing? 25 00:01:50,540 --> 00:01:53,330 If it doesn't, then it's going to look inside of 26 00:01:53,330 --> 00:01:56,495 that instances class and it's going to ask 27 00:01:56,495 --> 00:02:00,155 does that instances class have that instance variable or method? 28 00:02:00,155 --> 00:02:05,390 If it doesn't, then it's then going to look into the superclass. 29 00:02:05,390 --> 00:02:08,990 In Python, that's just how superclasses work. 30 00:02:08,990 --> 00:02:12,500 By adding this extra layer of 31 00:02:12,500 --> 00:02:16,180 looking at the superclass for things that are missing in the subclass, 32 00:02:16,180 --> 00:02:21,200 what we do is we inherit everything that the superclass has and more. 33 00:02:21,200 --> 00:02:23,795 So, this gets to the question of, 34 00:02:23,795 --> 00:02:26,975 "When should we actually inherit from a superclass?" 35 00:02:26,975 --> 00:02:29,810 Well, you should only inherit if 36 00:02:29,810 --> 00:02:35,510 your subclass should have absolutely everything that the superclass has, 37 00:02:35,510 --> 00:02:40,325 plus more or maybe plus some small modification. 38 00:02:40,325 --> 00:02:43,340 So, let's go over an example of this. 39 00:02:43,340 --> 00:02:46,625 So, let's suppose that I want to represent books in the library, 40 00:02:46,625 --> 00:02:49,220 and let's suppose that we have two kinds of books. 41 00:02:49,220 --> 00:02:52,565 We have paper books and we have ebooks. 42 00:02:52,565 --> 00:02:55,040 So, the way that I would represent that in classes, 43 00:02:55,040 --> 00:02:57,650 is I would have one superclass for book. 44 00:02:57,650 --> 00:03:00,720 So, I'd say class book, 45 00:03:02,900 --> 00:03:07,970 and every book is going to have a title and an author. 46 00:03:07,970 --> 00:03:12,125 So, I'll say def_init, 47 00:03:12,125 --> 00:03:16,280 and that's going to take in a title and an author. 48 00:03:16,280 --> 00:03:25,605 Then I'm just going to set self.title,.title equals title, self.author equals author. 49 00:03:25,605 --> 00:03:30,225 Let's suppose that we have this _str method as well, 50 00:03:30,225 --> 00:03:37,390 and we're going to return title by author. 51 00:03:40,640 --> 00:03:44,375 Okay, so now I have this book class. 52 00:03:44,375 --> 00:03:46,505 I can create a new book. 53 00:03:46,505 --> 00:03:52,470 So, I'll say, myBook equals a new Book. 54 00:03:52,870 --> 00:03:57,810 Let's suppose that the title is 55 00:03:57,810 --> 00:04:05,880 The Odyssey by Homer. 56 00:04:11,050 --> 00:04:15,850 Then if I print out myBook, 57 00:04:16,040 --> 00:04:20,050 then I'll see that my book is The Odyssey by Homer. 58 00:04:20,050 --> 00:04:26,745 Let me actually add quotation marks around this when we print out the book title. 59 00:04:26,745 --> 00:04:31,255 Okay, now let's suppose that a library can have two kinds of books. 60 00:04:31,255 --> 00:04:34,495 Let's suppose that it can have ebooks and paper books. 61 00:04:34,495 --> 00:04:40,315 So, both ebooks and paper books have everything that a book has plus more. 62 00:04:40,315 --> 00:04:42,010 In the case of a paper book, 63 00:04:42,010 --> 00:04:43,720 maybe we have a number of pages. 64 00:04:43,720 --> 00:04:48,890 So, I'll say class PaperBook inherits from Book. 65 00:04:49,560 --> 00:04:59,820 I'm going to have a constructor that takes in a title, author, and numPages. 66 00:04:59,820 --> 00:05:02,745 Then I'm going to call the Book constructor. 67 00:05:02,745 --> 00:05:10,170 So, I'll say, book._init with self, title, and author. 68 00:05:10,170 --> 00:05:13,410 Then I'm going to assign an instance variable numPages. 69 00:05:13,410 --> 00:05:19,290 So, I'll say self.numPages equals numPages. 70 00:05:19,290 --> 00:05:21,300 So, that's a paper book. 71 00:05:21,300 --> 00:05:25,200 Now, let's suppose that an electronic book doesn't have pages, 72 00:05:25,200 --> 00:05:26,780 but it has a file size, 73 00:05:26,780 --> 00:05:29,020 like three megabytes, five megabytes. 74 00:05:29,020 --> 00:05:30,955 Hopefully not a gigabyte for a book, 75 00:05:30,955 --> 00:05:32,860 but it has some size. 76 00:05:32,860 --> 00:05:37,740 So, I'll say class EBook inherits from Book. 77 00:05:37,740 --> 00:05:39,120 So, just like a book, 78 00:05:39,120 --> 00:05:41,630 an e-book is going to have a title and an author. 79 00:05:41,630 --> 00:05:48,120 I'll say def_init_ self, title, author. 80 00:05:48,120 --> 00:05:51,420 Then instead of numPages, it has size. 81 00:05:51,420 --> 00:05:55,670 Again, that's going to be the number of megabytes that this ebook takes up. 82 00:05:55,670 --> 00:06:03,450 So, I'll say Book.init self, title, and author. 83 00:06:03,450 --> 00:06:07,110 I'll say self.size equals size. 84 00:06:07,110 --> 00:06:11,870 Okay. So, now let's say that my book is 85 00:06:11,870 --> 00:06:18,630 an e-book with The Odyssey by Homer and let's say that it's two megabytes. 86 00:06:18,630 --> 00:06:21,335 So, if I print out my ebook, 87 00:06:21,335 --> 00:06:25,105 then I can see that it's still The Odyssey by Homer. 88 00:06:25,105 --> 00:06:28,295 If I print out my book.size, 89 00:06:28,295 --> 00:06:31,420 then I can see that it's two megabytes. 90 00:06:31,420 --> 00:06:34,000 If this is instead a paper book, 91 00:06:34,000 --> 00:06:39,325 so let's suppose that I also have a paper copy of this book, 92 00:06:39,325 --> 00:06:44,220 myPaperBook equals new PaperBook, 93 00:06:44,220 --> 00:06:45,690 The Odyssey by Homer, 94 00:06:45,690 --> 00:06:49,570 and let's say that it has 500 pages. 95 00:06:49,640 --> 00:06:57,900 I can see that myPaperBook.size. 96 00:06:57,900 --> 00:06:59,640 It doesn't have size, 97 00:06:59,640 --> 00:07:02,085 instead it has numPages. 98 00:07:02,085 --> 00:07:07,110 Paper books don't have size, they have numPages. 99 00:07:07,110 --> 00:07:12,000 So, you can see that myPaperBook.numPages is 500. 100 00:07:12,000 --> 00:07:14,470 So, in this example, 101 00:07:14,470 --> 00:07:20,630 we wanted to create paper book and ebook as separate subclasses because in this case, 102 00:07:20,630 --> 00:07:23,210 a paper book has everything that a book has. 103 00:07:23,210 --> 00:07:26,600 So, in this case, a paper book has a title and an author, 104 00:07:26,600 --> 00:07:28,110 and so does ebook. 105 00:07:28,110 --> 00:07:30,440 So, ebook has everything that a book has. 106 00:07:30,440 --> 00:07:34,820 So, it has title and author and paper book also has this additional thing, 107 00:07:34,820 --> 00:07:39,840 numPages and e-book has this additional thing bio size. 108 00:07:40,850 --> 00:07:44,985 So, that's when you want to inherit. 109 00:07:44,985 --> 00:07:49,970 When you want to have a subclass that has everything that the superclass has, plus more. 110 00:07:49,970 --> 00:07:52,115 When you don't want to inherit, 111 00:07:52,115 --> 00:07:56,260 is when you want to contain another class within a class. 112 00:07:56,260 --> 00:07:58,410 So, let's go through an example. 113 00:07:58,410 --> 00:08:02,150 So, let's suppose that I don't want these books to exist in isolation. 114 00:08:02,150 --> 00:08:07,040 Let's say that I want to have a library class and libraries are going to contain books. 115 00:08:07,040 --> 00:08:08,765 So, what I'm going to do, 116 00:08:08,765 --> 00:08:10,375 is I'm going to say, 117 00:08:10,375 --> 00:08:19,295 class library and every library has some fixed number of books. 118 00:08:19,295 --> 00:08:24,130 So, I'll represent that by saying def_init. 119 00:08:24,130 --> 00:08:28,850 So, I'm going to say that every library starts off with zero books. 120 00:08:28,850 --> 00:08:32,540 So, I'll say self.books is an empty list. 121 00:08:32,540 --> 00:08:35,930 Then we can add books to our library catalog. 122 00:08:35,930 --> 00:08:41,290 So, I'll say def addBook self, and then book. 123 00:08:41,290 --> 00:08:44,300 Here, book is going to be an instance of book. 124 00:08:44,300 --> 00:08:45,890 So, whether it's a book, 125 00:08:45,890 --> 00:08:47,320 a paper book or an ebook. 126 00:08:47,320 --> 00:08:48,590 What we do is, 127 00:08:48,590 --> 00:08:50,885 we add that book to our list of books. 128 00:08:50,885 --> 00:08:52,675 So, I'll say self.books.append(book). 129 00:08:52,675 --> 00:08:59,850 I'll say def getSize. 130 00:08:59,850 --> 00:09:03,225 This is going to return the number of books that we have. 131 00:09:03,225 --> 00:09:08,095 So, I'll return the length of self.book. 132 00:09:08,095 --> 00:09:12,980 I'll actually give this a clear name, getNumBooks. 133 00:09:14,130 --> 00:09:19,930 Okay, so the important point that I want to highlight here is that, 134 00:09:19,930 --> 00:09:25,250 even though there's some relationship between library and this book classes, 135 00:09:25,250 --> 00:09:28,555 in that a library contains a list of books, 136 00:09:28,555 --> 00:09:32,560 we didn't want to inherit from book or we didn't want book to inherit 137 00:09:32,560 --> 00:09:36,430 from library because it's not that a library has everything that a book has, 138 00:09:36,430 --> 00:09:40,660 it doesn't, or that a book has everything library has, it doesn't either. 139 00:09:40,660 --> 00:09:43,030 It's just that libraries contain books. 140 00:09:43,030 --> 00:09:45,550 So, rather than inheriting from books, 141 00:09:45,550 --> 00:09:48,400 I created a separate instance variable 142 00:09:48,400 --> 00:09:52,410 to actually contain this list of books that we have. 143 00:09:52,410 --> 00:09:55,300 So, this is an example of what's called composition. 144 00:09:55,300 --> 00:09:59,325 So, a library contains or is composed of a list of books, 145 00:09:59,325 --> 00:10:02,240 but rather than inheriting from the book class, 146 00:10:02,240 --> 00:10:06,520 then we want to have a list of books as our instance variable. 147 00:10:06,520 --> 00:10:09,100 So, let's see our class in action. 148 00:10:09,100 --> 00:10:12,925 So, let's suppose that I have the Ann Arbor District Library, 149 00:10:12,925 --> 00:10:18,465 aadl equals a new library and I say 150 00:10:18,465 --> 00:10:27,040 aadl.addBook(myBook), and aadl.addBook (myPaperBook). 151 00:10:27,560 --> 00:10:33,735 Now, if I print out aadl.getNumBooks, 152 00:10:33,735 --> 00:10:38,330 then I'm going to see that my District Library has two books. 153 00:10:38,330 --> 00:10:42,000 That's all for now, until next time.