Skip to content

Commit e0fcc52

Browse files
committed
updated
1 parent a81c3db commit e0fcc52

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

8. OOPS/4.Encapsulation.ipynb

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Encapsulation"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). 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. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variables.\n",
15+
"\n",
16+
"\n",
17+
"Protected members (in C++ 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. To accomplish this in Python, just follow the convention by prefixing the name of the member by a single underscore “_”.\n",
18+
"\n",
19+
"Although the protected variable can be accessed out of the class as well as in the derived class (modified too in derived class), it is customary(convention not a rule) to not access the protected out the class body."
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 2,
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"'JAMEEL'"
31+
]
32+
},
33+
"execution_count": 2,
34+
"metadata": {},
35+
"output_type": "execute_result"
36+
}
37+
],
38+
"source": [
39+
"### Encapsulation with Getter and Setter MEthods\n",
40+
"### Public,protected,private variables or access modifiers\n",
41+
"\n",
42+
"class Person:\n",
43+
" def __init__(self,name,age):\n",
44+
" self.name=name # public variables\n",
45+
" self.age=age # public variables\n",
46+
"\n",
47+
"def get_name(person):\n",
48+
" return person.name\n",
49+
"\n",
50+
"person=Person(\"JAMEEL\",25)\n",
51+
"get_name(person)"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 3,
57+
"metadata": {},
58+
"outputs": [
59+
{
60+
"name": "stdout",
61+
"output_type": "stream",
62+
"text": [
63+
"JAMEEL\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"# Protected variables we use with single underscore and can be used in inheritanced class but not outside of class\n",
69+
"class Person:\n",
70+
" def __init__(self,name,age,gender):\n",
71+
" self._name=name # protected variables\n",
72+
" self._age=age # protected variables\n",
73+
" self.gender=gender\n",
74+
"\n",
75+
"class Employee(Person):\n",
76+
" def __init__(self,name,age,gender):\n",
77+
" super().__init__(name,age,gender)\n",
78+
"\n",
79+
"\n",
80+
"employee=Employee(\"JAMEEL\",25,\"Male\")\n",
81+
"print(employee._name)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 5,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"name": "stdout",
91+
"output_type": "stream",
92+
"text": [
93+
"JAMEEL\n",
94+
"25\n",
95+
"27\n",
96+
"Age cannot be negative.\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"## Encapsulation With Getter And Setter\n",
102+
"class Person:\n",
103+
" def __init__(self,name,age):\n",
104+
" self.__name=name ## Private access modifier or variable\n",
105+
" self.__age=age ## Private variable\n",
106+
"\n",
107+
" ## getter method for name\n",
108+
" def get_name(self):\n",
109+
" return self.__name\n",
110+
" \n",
111+
" ## setter method for name\n",
112+
" def set_name(self,name):\n",
113+
" self.__name=name\n",
114+
"\n",
115+
" # Getter method for age\n",
116+
" def get_age(self):\n",
117+
" return self.__age\n",
118+
" \n",
119+
" # Setter method for age\n",
120+
" def set_age(self, age):\n",
121+
" if age > 0:\n",
122+
" self.__age = age\n",
123+
" else:\n",
124+
" print(\"Age cannot be negative.\")\n",
125+
"\n",
126+
"\n",
127+
"person=Person(\"JAMEEL\",25)\n",
128+
"\n",
129+
"## Access and modify private variables using getter and setter\n",
130+
"\n",
131+
"print(person.get_name())\n",
132+
"print(person.get_age())\n",
133+
"\n",
134+
"person.set_age(27)\n",
135+
"print(person.get_age())\n",
136+
"\n",
137+
"person.set_age(-5)\n",
138+
" "
139+
]
140+
}
141+
],
142+
"metadata": {
143+
"kernelspec": {
144+
"display_name": "Python 3",
145+
"language": "python",
146+
"name": "python3"
147+
},
148+
"language_info": {
149+
"codemirror_mode": {
150+
"name": "ipython",
151+
"version": 3
152+
},
153+
"file_extension": ".py",
154+
"mimetype": "text/x-python",
155+
"name": "python",
156+
"nbconvert_exporter": "python",
157+
"pygments_lexer": "ipython3",
158+
"version": "3.10.0"
159+
}
160+
},
161+
"nbformat": 4,
162+
"nbformat_minor": 2
163+
}

0 commit comments

Comments
 (0)