8 4-Encapsulation
8 4-Encapsulation
ipynb - Colab
Encapsulation and abstraction are two fundamental principles of Object-Oriented Programming (OOP) that help in designing robust,
maintainable, and reusable code. Encapsulation involves bundling data and methods that operate on the data within a single unit, while
abstraction involves hiding complex implementation details and exposing only the necessary features.
keyboard_arrow_down Encapsulation
Encapsulation is the concept of wrapping data (variables) and methods (functions) together as a single unit. It restricts direct access to some
of the object's components, which is a means of preventing accidental interference and misuse of the data.
class Person:
def __init__(self,name,age):
self.name=name ## public variables
self.age=age ## public variables
def get_name(person):
return person.name
person=Person("Krish",34)
get_name(person)
'Krish'
dir(person)
['__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'age',
'name']
class Person:
def __init__(self,name,age,gender):
self.__name=name ## private variables
self.__age=age ## private variables
self.gender=gender
def get_name(person):
return person.__name
person=Person("Krish",34,"Male")
get_name(person)
https://colab.research.google.com/drive/14ZnkuOTCy_Tk5q8lTaD9aT-XGQXUylYe#printMode=true 1/3
7/18/24, 10:41 AM 8.4-Encapsulation.ipynb - Colab
dir(person)
['_Person__age',
'_Person__name',
'__class__',
'__delattr__',
'__dict__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getstate__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__module__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'gender']
class Person:
def __init__(self,name,age,gender):
self._name=name ## protected variables
self._age=age ## protected variables
self.gender=gender
class Employee(Person):
def __init__(self,name,age,gender):
super().__init__(name,age,gender)
employee=Employee("KRish",34,"Male")
print(employee._name)
KRish
https://colab.research.google.com/drive/14ZnkuOTCy_Tk5q8lTaD9aT-XGQXUylYe#printMode=true 2/3
7/18/24, 10:41 AM 8.4-Encapsulation.ipynb - Colab
## Encapsulation With Getter And Setter
class Person:
def __init__(self,name,age):
self.__name=name ## Private access modifier or variable
self.__age=age ## Private variable
## Access
Start andor
coding modify private
generate withvariables
AI. using getter and setter
print(person.get_name())
print(person.get_age())
person.set_age(35)
print(person.get_age())
person.set_age(-5)
Krish
34
35
Age cannot be negative.
https://colab.research.google.com/drive/14ZnkuOTCy_Tk5q8lTaD9aT-XGQXUylYe#printMode=true 3/3