Concept of Encapsulation in OOPS in Python
* Encapsulation is a mechanism where the data ( variables ) and
the code ( methods ) that act on the data are bound together .
* For example , if we consider a class , then we can write the
variables and methods inside the class . Therefore , a class is binding them
together . Hence , a class is an example for encapsulation .
* The variables and methods of a class are called as 'members'
of the class . All the members of a class are by default available outside the
class . This means that all the class members are public by default . Public means
available to other programs and classes .
* Python follows Uniform
Access Principle that says that in OOPS all
the members of the class whether they are variables or methods should be
accessible in a uniform manner .
* Therefore , Python variables and methods are available outside
alike . This means both the variables are public by default .
* Usually in C++ and java type languages , - variables are kept
private which means that they are not available outside the class and the methods are kept public which means that they are
available to other programs
* But in Python , both the variables and methods are public by
default
* Encapsulation isolates the members of a class from the members
of another class . The reason is that when objects are created , each object
shares different memory and hence there will not be any overwriting of data .
* This gives an advantage to the programmer to use same names
for the members of two different classes . For example , a programmer can
declare and use the variables like 'id' , 'name' and 'address' in different
classes like Employee , Customer or Student Class .
No comments:
Post a Comment