1 00:00:00,009 --> 00:00:03,509 Another very important concept of object oriented programming 2 00:00:03,720 --> 00:00:06,489 is going to be the concept of Inheritance. 3 00:00:06,500 --> 00:00:09,800 And just like in the English language, we know Inheritance to be when 4 00:00:10,140 --> 00:00:14,050 you know something is passed on from one person to another, right? 5 00:00:14,369 --> 00:00:14,880 So 6 00:00:15,060 --> 00:00:20,059 in Python, we can actually create specialized versions of a class 7 00:00:20,389 --> 00:00:22,870 without even having to rewrite code, 8 00:00:23,129 --> 00:00:26,170 we can basically create a child class that can 9 00:00:26,180 --> 00:00:31,219 inherit attributes and methods from a parent class. 10 00:00:31,500 --> 00:00:35,970 So I want to give you an example in here since we're dealing with the class user 11 00:00:36,409 --> 00:00:40,740 obviously in systems and networks, we have different levels of permissions, 12 00:00:40,750 --> 00:00:41,029 right? 13 00:00:41,319 --> 00:00:46,689 A regular user may be able to access certain kinds of files and then print those files 14 00:00:46,819 --> 00:00:48,889 while an admin user 15 00:00:49,169 --> 00:00:53,060 will be able to do those exact same things that a regular user can. 16 00:00:53,080 --> 00:00:55,319 But because they're an admin user, 17 00:00:55,459 --> 00:00:58,220 they will have extra permissions like being able to say for example, 18 00:00:58,229 --> 00:01:00,009 edit those files, right? 19 00:01:00,020 --> 00:01:00,369 So 20 00:01:00,639 --> 00:01:07,800 what I'm gonna do right now is I'm gonna create a class for the regular user. 21 00:01:08,080 --> 00:01:08,970 But then 22 00:01:09,099 --> 00:01:13,860 I will create another class which will, which will be the admin user 23 00:01:14,099 --> 00:01:15,669 that will inherit 24 00:01:15,790 --> 00:01:20,470 the methods from the regular user. So over here 25 00:01:20,580 --> 00:01:22,220 we have a class user 26 00:01:22,550 --> 00:01:26,620 and now I'm defining one basic attribute 27 00:01:26,769 --> 00:01:29,809 and that's going to be the username. OK? 28 00:01:30,000 --> 00:01:35,660 So now the next step would just be to simply say self dot username 29 00:01:35,830 --> 00:01:36,900 equals 30 00:01:38,629 --> 00:01:41,470 equals. And then of course, user name. 31 00:01:41,699 --> 00:01:44,669 Now I want to create a method 32 00:01:44,849 --> 00:01:49,150 specifically for our user over here. So I'm gonna say define 33 00:01:49,599 --> 00:01:54,870 and let's say a regular user can log in, right? They can log in. So I'm gonna say log in 34 00:01:55,669 --> 00:01:57,169 and now in brackets, 35 00:01:57,339 --> 00:01:58,800 I'm gonna say self, 36 00:01:59,489 --> 00:01:59,970 OK? 37 00:01:59,980 --> 00:02:01,739 And then when they log in, let's just do something, 38 00:02:01,750 --> 00:02:04,230 let's say print and then self username 39 00:02:04,690 --> 00:02:06,589 has logged 40 00:02:06,690 --> 00:02:08,508 in. Let me say has, 41 00:02:09,679 --> 00:02:11,110 has some space in here, 42 00:02:11,229 --> 00:02:12,380 has logged in. 43 00:02:12,639 --> 00:02:13,300 OK? 44 00:02:13,750 --> 00:02:18,270 So far what we've done here is we've created the user class 45 00:02:18,460 --> 00:02:24,130 with the attribute username. We've also assigned a method called login 46 00:02:24,339 --> 00:02:27,259 that will basically print out the user's name 47 00:02:27,550 --> 00:02:29,020 has logged in. 48 00:02:29,850 --> 00:02:30,910 However, 49 00:02:31,380 --> 00:02:32,600 I am now 50 00:02:32,779 --> 00:02:34,320 gonna create 51 00:02:34,619 --> 00:02:38,289 another class that's going to inherit 52 00:02:38,550 --> 00:02:42,699 not only the attributes username from, from the user class, 53 00:02:42,710 --> 00:02:45,919 but also the method log in. 54 00:02:46,050 --> 00:02:49,080 How am I gonna do this? I'm gonna come over here 55 00:02:49,470 --> 00:02:50,490 and then notice the intent. 56 00:02:50,699 --> 00:02:52,089 I'm all the way out. 57 00:02:52,220 --> 00:02:54,440 Now, I'm gonna say class 58 00:02:55,830 --> 00:03:00,889 and now provide the name of this new class. So I'm gonna say admin 59 00:03:02,339 --> 00:03:05,360 and then we can even say admin user as an example. OK? 60 00:03:05,369 --> 00:03:09,679 Actually let me just stick with admin. OK? So admin and now in brackets, 61 00:03:10,050 --> 00:03:11,559 I'm gonna say user 62 00:03:12,770 --> 00:03:13,539 colon. 63 00:03:13,899 --> 00:03:16,550 So right over here on line nine, 64 00:03:16,669 --> 00:03:21,070 we have created a new class of a user called admin user 65 00:03:21,369 --> 00:03:23,229 that's going to inherit 66 00:03:23,440 --> 00:03:30,270 the attributes, username. And then the method log in from the user class. 67 00:03:30,669 --> 00:03:33,750 I'm gonna go one step further and now add 68 00:03:33,759 --> 00:03:36,100 an additional method because this is an admin user, 69 00:03:36,110 --> 00:03:37,220 they can do extra things. 70 00:03:37,229 --> 00:03:38,509 So I'm gonna say define 71 00:03:39,179 --> 00:03:41,009 and let's call this access 72 00:03:41,399 --> 00:03:43,679 on those call logs because they're an admin, 73 00:03:43,850 --> 00:03:47,419 they can access the logs. So I'm gonna say self call on 74 00:03:47,809 --> 00:03:49,880 and now let's just print 75 00:03:50,210 --> 00:03:51,839 uh print 76 00:03:52,630 --> 00:03:53,740 and then in brackets, 77 00:03:54,179 --> 00:03:58,270 I can use F string and let's say something like uh 78 00:03:59,000 --> 00:03:59,929 the self 79 00:04:00,600 --> 00:04:01,389 dot 80 00:04:01,660 --> 00:04:02,539 username. 81 00:04:03,410 --> 00:04:04,139 OK? 82 00:04:04,779 --> 00:04:07,399 Is accessing the logs 83 00:04:08,880 --> 00:04:09,679 and 84 00:04:09,789 --> 00:04:12,960 there it is and there you go, we are done. 85 00:04:13,100 --> 00:04:18,619 So now what I'm gonna do is I'm gonna create our admin user. 86 00:04:18,899 --> 00:04:20,600 So I'm gonna say admin 87 00:04:21,160 --> 00:04:24,220 equals and now in capital admin user 88 00:04:25,149 --> 00:04:27,450 and then in broadcast, let's provide the username, 89 00:04:28,040 --> 00:04:28,929 admin. 90 00:04:29,959 --> 00:04:30,579 OK? 91 00:04:31,600 --> 00:04:32,540 And now 92 00:04:32,880 --> 00:04:35,059 let us run 93 00:04:35,309 --> 00:04:36,459 the methods. 94 00:04:36,579 --> 00:04:38,459 So I'm gonna say admin 95 00:04:38,970 --> 00:04:40,119 dot log in 96 00:04:40,459 --> 00:04:41,440 brackets. 97 00:04:41,750 --> 00:04:45,570 And let's also run the second method which is gonna be admin dot 98 00:04:46,209 --> 00:04:48,589 access underscore logs 99 00:04:49,070 --> 00:04:50,070 and there it is. 100 00:04:50,329 --> 00:04:54,109 So now let's see if this will actually work. Let's run 101 00:04:54,429 --> 00:04:56,160 and 0000 102 00:04:56,359 --> 00:04:58,809 I have another error 103 00:04:58,940 --> 00:05:00,149 admin. Oh, 104 00:05:00,399 --> 00:05:01,549 I do apologize. 105 00:05:02,429 --> 00:05:05,410 It should be admin over here. I got carried away. 106 00:05:05,529 --> 00:05:08,269 Admin equals admin and then the username admin. 107 00:05:08,420 --> 00:05:12,279 I do apologize. So let me move on the program one more time and there you go. All right. 108 00:05:12,290 --> 00:05:13,869 So admin has logged in 109 00:05:14,089 --> 00:05:17,730 and now admin is accessing the logs and 110 00:05:17,880 --> 00:05:19,339 that's basically it. 111 00:05:19,670 --> 00:05:24,850 So we didn't have to specify that the admin user, the class admin, 112 00:05:25,540 --> 00:05:26,609 it's gonna have 113 00:05:26,839 --> 00:05:31,630 the parameter or the attribute username or the method I log in 114 00:05:31,709 --> 00:05:34,420 all we had to do was to indicate to Python that, 115 00:05:34,429 --> 00:05:34,980 hey, 116 00:05:35,119 --> 00:05:39,000 this new class admin is actually inheriting 117 00:05:39,450 --> 00:05:42,920 the attributes and methods of the original 118 00:05:43,200 --> 00:05:44,959 user class. 119 00:05:45,250 --> 00:05:49,239 And that's Inheritance for you. Thank you for watching the video. 120 00:05:49,250 --> 00:05:51,079 I will see you in the next class.