1 00:00:02,210 --> 00:00:10,100 In this tutorial we will discuss about encapsulation an important part of object oriented programming 2 00:00:10,280 --> 00:00:21,080 is d encapsulation feature encapsulation is used to hide the values of these gate all or data object 3 00:00:21,320 --> 00:00:27,460 inside a class preventing unauthorized access to them. 4 00:00:28,920 --> 00:00:39,660 So encapsulation is about ensuring the safe storage of data data should be safe from changes by external 5 00:00:39,660 --> 00:00:46,170 processes the encapsulation feature involves grouping attributes 6 00:00:48,730 --> 00:00:57,170 and method into packages that contain different forms of access. 7 00:00:57,250 --> 00:01:09,090 For example certain methods an attribute are packaged as public and so these attributes and methods 8 00:01:09,330 --> 00:01:18,990 can be accessed by any class next Some attribute and method. 9 00:01:20,330 --> 00:01:30,980 Packaged as protected that means only the child classes can access these attributes and methods which 10 00:01:30,980 --> 00:01:41,090 are defined in the bad and class nixed certain attributes and methods are packaged as private which 11 00:01:41,090 --> 00:01:47,300 means the methods and attributes can only be accessed by the class itself. 12 00:01:49,740 --> 00:02:00,990 Now coming to the encapsulation feature in Python encapsulation and Python lacks strict access control 13 00:02:01,620 --> 00:02:11,620 such as private and protected attributes Python interpreter will stop us from accidentally accessing 14 00:02:11,980 --> 00:02:14,240 the attribute and the method. 15 00:02:14,710 --> 00:02:24,950 But we can intentionally do anything as long as we are aware how the language works so in Python almost 16 00:02:25,070 --> 00:02:36,440 everything about classes and objects is open open or public so we can easily inspect object Internet 17 00:02:37,280 --> 00:02:47,540 and the objects are all accessible without any restrictions everything that is defined in the base class 18 00:02:47,720 --> 00:02:58,660 is inherited and accessible from derived class and this exposes in done the implementation of an object 19 00:02:59,720 --> 00:03:01,700 let us now see an example. 20 00:03:02,180 --> 00:03:11,000 So we have the Department class which is the parent class of the superclass and we have another class 21 00:03:11,060 --> 00:03:17,270 employee which is the derived class of the class department. 22 00:03:17,300 --> 00:03:26,790 Now let us create an instance of the department class let's say the department is accounting. 23 00:03:26,800 --> 00:03:29,730 This is an instance of the department class. 24 00:03:29,890 --> 00:03:41,760 So let us access the attribute defined in this class using the instance accounting dot I.D. So here 25 00:03:42,370 --> 00:03:44,790 is the value for this attribute. 26 00:03:44,800 --> 00:03:50,900 Now we can change the value of this attribute using these instance. 27 00:03:51,220 --> 00:04:00,820 So let's give another while due to this attribute and then display the attribute using the instance. 28 00:04:00,820 --> 00:04:12,540 So we weren't able to change the value of an attribute using the instant so without interacting with 29 00:04:12,660 --> 00:04:19,450 any or the method in a class we would able to change the value of an attribute. 30 00:04:19,710 --> 00:04:25,620 And this is called breaking encapsulation and it is a bad practice for a programmer. 31 00:04:26,770 --> 00:04:34,660 Because the class are the odd maintainer can no longer validate the data that is being accessed. 32 00:04:34,900 --> 00:04:42,670 And this can cause unforeseen problems with a program that is dependent on the class it is inheriting 33 00:04:42,670 --> 00:04:51,610 from so object where tables are attributes should not always be directly accessible to prevent accidental 34 00:04:51,610 --> 00:04:52,600 change. 35 00:04:52,600 --> 00:05:01,150 The attribute can sometimes only be change using the method the methods can ensure the correct values 36 00:05:01,240 --> 00:05:02,110 are set. 37 00:05:02,230 --> 00:05:11,520 And if an incorrect value is set the method can return and error python and relies on programming conventions 38 00:05:11,610 --> 00:05:17,190 to indicate the intended use of an object. 39 00:05:17,190 --> 00:05:26,490 So instead of having to enforce language rules it is left to the programmer to observe conventions and 40 00:05:26,490 --> 00:05:29,980 these conventions are based on naming. 41 00:05:30,090 --> 00:05:40,720 So let's understand this Python doesn't have the concept of private method there are no arbitrary restrictions 42 00:05:40,780 --> 00:05:48,380 when accessing part of a class so there are no ways to impose a metered out at tribute to be strictly 43 00:05:48,380 --> 00:05:48,910 private. 44 00:05:50,750 --> 00:05:58,340 But to be able to have the encapsulation feature Python has a concept of hidden variables in which Python 45 00:05:58,340 --> 00:06:05,900 does not restrict access but hinders access to certain variables by mutilating them. 46 00:06:06,770 --> 00:06:16,340 Let us now see if you example to understand how Python uses naming convention to hinder access to attribute 47 00:06:16,450 --> 00:06:17,790 or method. 48 00:06:18,350 --> 00:06:27,820 So here we have defined a class test 1 and there is a variable defined in the init method of this class. 49 00:06:27,950 --> 00:06:36,440 So if you observe there is a leading underscored a single leading underscored before the variable name 50 00:06:38,030 --> 00:06:41,550 and when you see a variable. 51 00:06:41,630 --> 00:06:50,820 Having a single leading underscore that means we should stay away from accessing these attribute or 52 00:06:50,900 --> 00:07:02,230 methods but that does not mean that we cannot access these attributes so let us try accessing this Redburn 53 00:07:02,990 --> 00:07:06,970 underscored a using an instance of this class. 54 00:07:07,070 --> 00:07:08,480 Text one. 55 00:07:08,480 --> 00:07:17,690 So we have created an instance of the test of the class test 1 and access this attribute containing 56 00:07:17,780 --> 00:07:27,170 a single leading underscore and we were able to read the content of this variable and we can also change 57 00:07:27,200 --> 00:07:33,860 the value to which this readable are the attributes pointing to two letters. 58 00:07:33,860 --> 00:07:41,640 Go ahead and change the value of this variable and then display in Sweden. 59 00:07:42,200 --> 00:07:49,730 So even though this variable has been marked as private using a single leading underscore we were able 60 00:07:49,730 --> 00:07:57,460 to change the value of this variable externally that is from outside the class. 61 00:07:57,540 --> 00:07:59,060 Let's see another example. 62 00:07:59,990 --> 00:08:03,380 And here's another class based too. 63 00:08:03,680 --> 00:08:06,450 In this class we have the init method. 64 00:08:06,620 --> 00:08:14,090 We have defined two variables or attribute in the init method which have been marked as private using 65 00:08:14,180 --> 00:08:24,470 naming convention one attribute has a leading single underscored and the added attribute has a leading 66 00:08:24,560 --> 00:08:26,380 double underscore. 67 00:08:26,400 --> 00:08:32,530 No let us create an instance of this class and try accessing both of these attributes. 68 00:08:32,540 --> 00:08:37,660 So here we have created an instance of the class. 69 00:08:37,670 --> 00:08:43,400 Now let us try accessing the private attributes defined in this class. 70 00:08:43,550 --> 00:08:47,060 So let us first access the variable B 71 00:08:49,480 --> 00:08:58,490 so we were able to see the content of this variable by directly accessing using an instance. 72 00:08:58,510 --> 00:09:07,880 Now let us try to access the second variable double underscore D. 73 00:09:07,880 --> 00:09:18,580 Now we have an exception drawn by the interpreter and the error is an attribute error and it says test 74 00:09:18,590 --> 00:09:23,500 to object has no attribute double underscore D. 75 00:09:24,260 --> 00:09:30,400 So in order to understand this error let us call the building function. 76 00:09:30,390 --> 00:09:39,220 D I add in order to see the list of attributes for the instance test case 2. 77 00:09:39,230 --> 00:09:45,260 So here we have the attributes for the instance test case 2. 78 00:09:45,770 --> 00:09:52,640 This is the first attribute underscored b a single leading underscore before the attribute name and 79 00:09:52,640 --> 00:09:57,170 then we also have the second attribute teal. 80 00:09:57,920 --> 00:10:08,600 So if you'll notice the double underscore D attribute has been mutilated or converted by adding the 81 00:10:08,600 --> 00:10:19,010 class name before the attribute any and the interpreter does this secretly and this concept of mutilating 82 00:10:19,100 --> 00:10:23,270 the attribute name is known as name mangling.