0% found this document useful (0 votes)
7 views3 pages

8 4-Encapsulation

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

8 4-Encapsulation

Uploaded by

AB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/18/24, 10:41 AM 8.4-Encapsulation.

ipynb - Colab

keyboard_arrow_down Encapsulation And Abstraction

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.

### Encapsulation with Getter and Setter MEthods


### Public,protected,private variables or access modifiers

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

## getter method for name


def get_name(self):
return self.__name

## setter method for name


def set_name(self,name):
self.__name=name

# Getter method for age


def get_age(self):
return self.__age

Start coding or generate with AI.


# Setter method for age
def set_age(self, age):
if age
Start coding or>generate
0: with AI.
self.__age = age
else:
Start codingprint("Age
or generate with AI.
cannot be negative.")

Start coding or generate with AI.


person=Person("Krish",34)

## 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

You might also like