Python Encapsulation

It describes the idea of wrapping data and the method that work on data within one unit.

class Supper: def __init__(self): self._value1 =100 #protected number self.__value2 =200 #private def display(self): print(self._value1) print(self.__value2) class Sub: def show(self): print(self._value1) print(self.__value2) obj = Sub() obj.show()

It will return an error beacuse self._value1 is protected variable and self.__value2 is private

-:Protected value can be call with a main class and its sub class

-:Private data can be access within a main class only

class Supper: def __init__(self): self._value1 =100 #protected number self.__value2 =200 #private def display(self): print(self._value1) print(self.__value2) class Sub(Supper): def show(self): print(self._value1) print(self.__value2) obj = Sub() obj.show()

It will return only 100 and second one will through error because protected data can be access by sub class but not private

X