Ticker

6/recent/ticker-posts

Encapsulation in Python:

 Encapsulation in Python:

Encapsulation is wrapping or bundling or packing the code and data into a single unit. It prevents from accidental modification of data and thus it provides security. It is also called as information hiding.

For example you cannot get all the information about employees, finance etc without getting proper permission from respective officers. Class is an example of encapsulation in python.

Access Modifiers in Python Encapsulation:

While doing encapsulation three types of access modifiers can be used. They are

1. Public Members

2. Private Members

3. Protected Members

Public Members:

This can be accessed from its own class, derived class and by object. So, it can be accessed from anywhere within or outside the program.

 

Public access in encapsulation in Python



Private Members (using single underscore):

Here the private variables are prefixed with single underscore. It doesn’t make much difference and it can be accesses as usual within the class.

 

Private member in encapsulation in Python

Here the age variable is prefixed by single underscore and it was accessed within the class and object. It can also be accessed from outside the class

Protected Members (using double underscore):

Here the protected variable is prefixed with double underscore. It can be accessed within the class but cannot be accessed outside the class.

 

Protected members in encapsulation in python



Here the age variable is prefixed with double underscore. it can be accessed within the class but when we try to access outside the class, we get an error.


Post a Comment

0 Comments