Learn-Object_Oriented_programming-with-us

Learn By Topic:

          

Class-objects

          

Inheritance

          

Polymorphism

          

Encapsulation

          

Abstraction

          

          

Click here to toggle b/w normal and focus mode: FOCUS MODE

What is Encapsulation ?

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP).Encapsulation in Python describes the concept of bundling data and methods within a single unit . Even creating a class is like encapsulation , as it builds all the data members (instance variables) and methods into a single unit.

Example
class students:
def __init__(self,name,intrest) :
self.name=name #data member
self.intrest=intrest #data member

#method
def prof(self)
print(self.name,'is intrested in'self.intrest)

In detailed , it describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data.

So, in short. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as PRAVITE.

Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section etc.

Access Modifiers

Encapsulation can be achived/initiated by declaring the data members and methods of a class either as private or protected. As we don’t have direct access modifiers like public, private, and protected. We can achieve this by using single underscore and double underscores.

Python provides three types of access modifiers private, public, and protected.

Example :
class student:
    def __init__(self):
        self.a = "Mowli"
        self._b=19

class display(student):
    def __init__(self):

        student.__init__(self)
        self.__c = "abcde@gmail.com"
        print("Calling private member of base class: ")

        print(self.__c)
        print("Calling protectwed member of base class:")
        print(self._b)


obj1 = display()
print(obj1.a)

Output:

Calling private member of base class: 
abcde@gmail.com
Calling protectwed member of base class:
19
Mowli

Protected Member

Protected members (in C++,Python and JAVA) are those members of the class that cannot be accessed outside the class but can be accessed from within the class and its subclasses.

As given in the above example , the Protected Member can only be accessed from the class itself or the class that inherited it.

Private Member

Private members are similar to protected members, the only difference between them is that the class members declared private should neither be accessed outside the class nor by any base class.

As you have seen in the above example , the Private Member is accessed only in the class it was initialized.

Setters and Getters

In python to implement proper encapsulation, we need to use setters and getters.The primary purpose of using getters and setters in object-oriented programs (OOP)is to ensure data encapsulation.

In Python, private variables are not hidden fields like in other programming languages. The getters and setters methods are often used when:

Advantages of Encapsulation

Thank you. Peace ✌️.


Made with ❤️ by SasiVatsal and Mowli