1 00:00:00,150 --> 00:00:07,920 In this session we will talk about the object oriented programming principle inheritance inheritance 2 00:00:08,040 --> 00:00:14,760 as the name suggests is a process of inheriting features. 3 00:00:14,830 --> 00:00:23,260 Inheritance is a technique of building new classes from an existing class by inheriting its features 4 00:00:23,920 --> 00:00:33,520 and the features include different attributes and methods let's understand inheritance. 5 00:00:33,520 --> 00:00:43,150 Using an example so here on the screen I have defined two classes on the left hand side of the screen. 6 00:00:43,180 --> 00:00:53,320 We have a class basic geek which defined and a net method which lets down the ingredient and there is 7 00:00:53,350 --> 00:01:02,740 another method procedure which describes the processes of the procedure to make a basic geek and to 8 00:01:02,740 --> 00:01:04,000 the right hand side. 9 00:01:04,120 --> 00:01:13,360 I have defined another class chocolate cake which has two methods which defined the stuffing deviant 10 00:01:13,600 --> 00:01:16,980 and then the procedure to make a chocolate cake. 11 00:01:18,590 --> 00:01:29,870 If you observe carefully most of the code in these classes has been repeated or duplicated and in the 12 00:01:29,870 --> 00:01:40,820 programming world duplicate code is not recommended we should not have multiple copies of the same or 13 00:01:40,820 --> 00:01:44,070 similar code in different places. 14 00:01:44,210 --> 00:01:53,500 And there are many ways to MOJ similar pieces of code or objects with similar functionality. 15 00:01:53,510 --> 00:01:59,360 In this example I have defined a class cake which again has two methods. 16 00:01:59,360 --> 00:02:11,240 The init method and then the procedure method and then I have also defined another class chocolate cake. 17 00:02:11,240 --> 00:02:24,860 But this time I have passed the class that I have defined about cake as a parameter to this new class. 18 00:02:24,910 --> 00:02:36,270 Chocolate cake and this is how we tell Python that the new class should be inherited or derived from 19 00:02:36,270 --> 00:02:40,000 the existing class. 20 00:02:40,070 --> 00:02:55,050 The new class is called child class or a subclass the existing class is called the parent class 21 00:02:58,490 --> 00:03:00,770 or superclass 22 00:03:03,100 --> 00:03:12,130 or the base class coming to our child class or subclass chocolate cake. 23 00:03:12,140 --> 00:03:24,570 I have defined and init method here and these are the parameters that I have passed to the child classes 24 00:03:25,080 --> 00:03:35,160 in that method and I have initialized the instance attribute self-taught cook or all the parameters 25 00:03:35,580 --> 00:03:44,430 flawed sugared Butter and eggs have already been initialized in the parent classes init method 26 00:03:47,820 --> 00:03:57,240 and now we have to make a call to the parent classes in a method in order to initialize all of these 27 00:03:58,050 --> 00:03:58,940 attributes. 28 00:03:58,980 --> 00:04:00,610 So how do we do that. 29 00:04:00,840 --> 00:04:09,570 And for this I'm going to use the super function the super function enables us to access the method 30 00:04:09,960 --> 00:04:20,380 and the attributes of a parent class without referring to the parent class by name using super. 31 00:04:20,670 --> 00:04:32,930 The instance of the parent object is retrieved post and then the init method is called on that object 32 00:04:33,350 --> 00:04:36,680 passing in the arguments here. 33 00:04:36,680 --> 00:04:46,760 The floor should good butter and eggs in another example the super function is a built in function which 34 00:04:46,850 --> 00:04:56,010 returns a proxy object that allows us to therefore paid in class by super so instead of calling the 35 00:04:56,020 --> 00:05:06,060 inert method using the last name we call the init method using the built in function super it so that 36 00:05:06,060 --> 00:05:11,790 we can easily change the in class name when required. 37 00:05:12,030 --> 00:05:20,430 So by inheriting the attributes from the parent class a lot of duplicate code has been awarded in the 38 00:05:20,520 --> 00:05:29,130 child class and inheritance enables us to define a child class that takes all the functionality from 39 00:05:29,190 --> 00:05:34,450 a parent class and also allows us to add more. 40 00:05:34,500 --> 00:05:37,890 So let's see some examples in the Jupiter notebook. 41 00:05:37,950 --> 00:05:47,070 Let's discuss another example in the Jupiter notebook your I have defined a class school and in this 42 00:05:47,070 --> 00:05:49,410 class there is an inert method. 43 00:05:50,010 --> 00:05:56,790 So these are the two parameters that we are opposed to the net method the school name and then the location 44 00:05:57,790 --> 00:06:06,880 and then we have initialized the instance attributes not let execute this in and then let us create 45 00:06:06,940 --> 00:06:11,770 an instance of this class school. 46 00:06:11,770 --> 00:06:24,710 So let's say the instance is a b c and then we will pass the the argument ABC school the location so 47 00:06:24,740 --> 00:06:32,280 ABC is an object of the class school. 48 00:06:32,370 --> 00:06:40,220 Now I am also going to create another class called teacher so let's do that. 49 00:06:40,410 --> 00:06:51,360 I have defined another class teacher and this time the existing class school has been passed as a parameter 50 00:06:52,080 --> 00:06:54,660 to the teacher class. 51 00:06:54,660 --> 00:07:05,160 So the last school is the parent class and the class teacher is the child class and in the new class 52 00:07:05,220 --> 00:07:08,790 or the child class I have defined and init method. 53 00:07:08,970 --> 00:07:18,090 The init method has these parameters the teacher name subject school name and location and then we have 54 00:07:18,090 --> 00:07:27,020 initialized all of these attributes and we know that we should not have multiple copies of the same 55 00:07:27,110 --> 00:07:30,390 or similar code in different places. 56 00:07:30,920 --> 00:07:40,430 So we need to have a way in order to make a call to the parent classes init method in order to initialize 57 00:07:40,430 --> 00:07:41,860 these attributes. 58 00:07:44,380 --> 00:07:48,850 And for that we can make use of the super function. 59 00:07:49,090 --> 00:08:00,500 So we are going to commend the duplicate code in the child classes in that method and then make a call 60 00:08:00,500 --> 00:08:03,950 to the parent classes in that method. 61 00:08:03,950 --> 00:08:08,490 For that let's use the super function S P E. 62 00:08:08,550 --> 00:08:20,480 So but DA and super returned the object as an instance of the parent class allowing us to call the parent 63 00:08:20,600 --> 00:08:22,360 method directly. 64 00:08:22,550 --> 00:08:33,800 So let's call the init method on the parent classes instant underscored underscored in it underscored 65 00:08:33,900 --> 00:08:44,080 underscore and we want to initialize the attribute school name and location. 66 00:08:44,420 --> 00:08:49,970 So using inheritance a lot of duplicate code has been removed. 67 00:08:51,440 --> 00:08:56,490 Now let's add another method to the to the parent class. 68 00:08:56,490 --> 00:09:05,400 Now to the parent class I have added a new method details which print the school name and the location. 69 00:09:05,400 --> 00:09:11,670 So here is the instance of the school class. 70 00:09:11,970 --> 00:09:14,780 So let's call the detailed method. 71 00:09:15,660 --> 00:09:18,860 ABC dot details. 72 00:09:19,020 --> 00:09:23,490 So here we have the details of the school. 73 00:09:23,530 --> 00:09:35,760 Now let us create an instance of the class teacher that is the child class so let's call this instance 74 00:09:35,760 --> 00:09:48,990 as Melinda and then the argument is to this glass that is a child loves the teacher name and the subject 75 00:09:50,220 --> 00:09:51,300 school name. 76 00:09:51,910 --> 00:09:57,760 A B C school and then location let's execute this. 77 00:09:57,770 --> 00:10:06,450 The class teacher is a child class and it has inherited the attributes and the methods of debate in 78 00:10:06,450 --> 00:10:07,840 class school. 79 00:10:07,860 --> 00:10:14,840 We can call the methods of the iPad and class which have not been defined in the child class so let 80 00:10:14,840 --> 00:10:15,570 us call. 81 00:10:15,660 --> 00:10:22,310 So let's call the details method using an instance of the child class. 82 00:10:22,560 --> 00:10:35,190 Melinda dot details let's call the method details using the child class instance executed. 83 00:10:35,270 --> 00:10:40,800 So you have the school name and the location displayed. 84 00:10:40,790 --> 00:10:45,860 Now I want to add a few more details. 85 00:10:46,010 --> 00:10:54,980 I want to add more code to the method details which also displays the name of the teacher and the subject 86 00:10:54,980 --> 00:10:58,450 that she teaches in the child class. 87 00:10:58,450 --> 00:11:10,260 I have added another method details and this method defined in the child class has the same name as 88 00:11:10,260 --> 00:11:14,630 the method independent class school. 89 00:11:14,670 --> 00:11:24,670 So what is going to happen if you call the detailed method on on a child classes instance. 90 00:11:24,960 --> 00:11:28,410 No let us call the details method. 91 00:11:29,820 --> 00:11:40,140 This time we have called the details method using the child classes instance and the detailed method 92 00:11:40,200 --> 00:11:43,320 of the child's class has been accessed. 93 00:11:44,040 --> 00:11:53,610 And this is known as overriding overriding is the ability of a class to change the implementation of 94 00:11:53,700 --> 00:12:05,350 a method provided by one of its parent classes so instead of implementing the method provided by the 95 00:12:05,350 --> 00:12:13,250 parent class that is the details method independent class using method or what writing. 96 00:12:13,330 --> 00:12:25,140 We have implemented the method that is defined in the child class so using inheritance we can implement 97 00:12:25,460 --> 00:12:28,950 the the method defined in the parent class. 98 00:12:29,040 --> 00:12:39,240 But if we all would write the method in the child class the methods provided by the better class will 99 00:12:39,240 --> 00:12:44,000 not be considered at all next. 100 00:12:44,070 --> 00:12:51,720 Say for example apart from displaying the teacher name and the subject you also want to display the 101 00:12:51,720 --> 00:12:59,880 school name and the location in the output menu implement the detailed method in the child class. 102 00:12:59,880 --> 00:13:09,870 That is the teacher class but we already have a method in the parent class school which through which 103 00:13:09,870 --> 00:13:13,730 we are displaying the school name and the location. 104 00:13:13,740 --> 00:13:22,060 We can make a call to the to the details method of the parent class using the super function so using 105 00:13:22,060 --> 00:13:30,710 the super function we can call the good and method of the parent class from inside the child. 106 00:13:30,710 --> 00:13:40,880 Class so as of now we cannot implement the details method of the parent class. 107 00:13:40,920 --> 00:13:49,170 That's because the details method of the child class is being implemented and both these methods have 108 00:13:49,170 --> 00:13:50,460 the same name. 109 00:13:50,460 --> 00:13:58,230 If you want to make a call to the overwritten method of the parent class you can use the super function. 110 00:13:58,230 --> 00:14:06,900 So not just in the insert method the super function can also be used in other methods. 111 00:14:07,020 --> 00:14:17,890 So super dart this time we are going to make a call to the details method of the in class and you're 112 00:14:17,890 --> 00:14:22,320 not going to pass any parameters. 113 00:14:22,400 --> 00:14:22,940 It's a good. 114 00:14:22,940 --> 00:14:34,210 This now when you call the details matter in the output we see all of the attributes displayed the first 115 00:14:34,210 --> 00:14:41,620 two attributes are being displayed using the details method of the child class and the last two attribute 116 00:14:41,620 --> 00:14:48,010 school name and location are being displayed using the details method of the parent class.