|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "34562270", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Classes & Objects\n", |
| 9 | + "- A Class is an object constructor or a \"blueprint\" for creating objects.<br>\n", |
| 10 | + "- Objects are nothing but an encapsulation of variables and functions into a single entity.<br>\n", |
| 11 | + "- Objects get their variables and functions from classes.<br>\n", |
| 12 | + "- To create a class we use the keyword class.<br>\n", |
| 13 | + "- The first string inside the class is called docstring which gives the brief description about the\n", |
| 14 | + " class.<br>\n", |
| 15 | + "- All classes have a function called which is always executed when the class is being initiated.<br>\n", |
| 16 | + "- We can use function to assign values to object properties or other operations that are necessary to perform when the object is being created<br>\n", |
| 17 | + "- The self parameter is a reference to the current instance of the class and is used to access\n", |
| 18 | + " class variables.<br>\n", |
| 19 | + "- self must be the first parameter of any function in the class<br>\n", |
| 20 | + "- The super() builtin function returns a temporary object of the superclass that allows us to access methods of the base class.<br>\n", |
| 21 | + "- super() allows us to avoid using the base class name explicitly and to enable multiple inheritance." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "id": "8492620c", |
| 28 | + "metadata": {}, |
| 29 | + "outputs": [ |
| 30 | + { |
| 31 | + "name": "stdout", |
| 32 | + "output_type": "stream", |
| 33 | + "text": [ |
| 34 | + "10\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "# Create a class with property \"var1\"\n", |
| 40 | + "class myclass:\n", |
| 41 | + " var1 = 10\n", |
| 42 | + "\n", |
| 43 | + "obj1 = myclass() # Create an object of class \"myclass()\"\n", |
| 44 | + "print(obj1.var1)" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": 2, |
| 50 | + "id": "cafb7464", |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [ |
| 53 | + { |
| 54 | + "name": "stdout", |
| 55 | + "output_type": "stream", |
| 56 | + "text": [ |
| 57 | + "Name :- Sunil\n", |
| 58 | + "Employee ID :- 942194\n", |
| 59 | + "Thanks for joining Neosoft Company Sunil!!\n" |
| 60 | + ] |
| 61 | + } |
| 62 | + ], |
| 63 | + "source": [ |
| 64 | + "#Create an employee class\n", |
| 65 | + "class Employee:\n", |
| 66 | + " def __init__(self, name, empid): # __init__() function is used to assign values\n", |
| 67 | + " self.name = name\n", |
| 68 | + " self.empid = empid\n", |
| 69 | + " def greet(self): # Class Method\n", |
| 70 | + " print(\"Thanks for joining Neosoft Company {}!!\".format(self.name))\n", |
| 71 | + "emp1 = Employee(\"Sunil\", 942194) # Create an employee object\n", |
| 72 | + "print('Name :- ',emp1.name)\n", |
| 73 | + "print('Employee ID :- ',emp1.empid)\n", |
| 74 | + "emp1.greet()" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 3, |
| 80 | + "id": "3a23246e", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "data": { |
| 85 | + "text/plain": [ |
| 86 | + "'Rohit'" |
| 87 | + ] |
| 88 | + }, |
| 89 | + "execution_count": 3, |
| 90 | + "metadata": {}, |
| 91 | + "output_type": "execute_result" |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "emp1.name = 'Rohit' # Modify Object Properties\n", |
| 96 | + "emp1.name" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 4, |
| 102 | + "id": "2b08a434", |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [ |
| 105 | + { |
| 106 | + "ename": "AttributeError", |
| 107 | + "evalue": "'Employee' object has no attribute 'empid'", |
| 108 | + "output_type": "error", |
| 109 | + "traceback": [ |
| 110 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 111 | + "\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", |
| 112 | + "Cell \u001b[1;32mIn[4], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m emp1\u001b[38;5;241m.\u001b[39mempid \u001b[38;5;66;03m# Delete Object Properties\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43memp1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mempid\u001b[49m\n", |
| 113 | + "\u001b[1;31mAttributeError\u001b[0m: 'Employee' object has no attribute 'empid'" |
| 114 | + ] |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "del emp1.empid # Delete Object Properties\n", |
| 119 | + "emp1.empid" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 5, |
| 125 | + "id": "f0ef5fac", |
| 126 | + "metadata": {}, |
| 127 | + "outputs": [ |
| 128 | + { |
| 129 | + "ename": "NameError", |
| 130 | + "evalue": "name 'emp1' is not defined", |
| 131 | + "output_type": "error", |
| 132 | + "traceback": [ |
| 133 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 134 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 135 | + "Cell \u001b[1;32mIn[5], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m emp1 \u001b[38;5;66;03m# Delete the object\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43memp1\u001b[49m\n", |
| 136 | + "\u001b[1;31mNameError\u001b[0m: name 'emp1' is not defined" |
| 137 | + ] |
| 138 | + } |
| 139 | + ], |
| 140 | + "source": [ |
| 141 | + "del emp1 # Delete the object\n", |
| 142 | + "emp1" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": 6, |
| 148 | + "id": "88c319b1", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "Name :- Kartik\n", |
| 156 | + "Employee ID :- 9970\n", |
| 157 | + "Thanks for joining Neosoft Company Kartik!!\n" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "emp2 = Employee(\"Kartik\", 9970) # Create an employee object\n", |
| 163 | + "print('Name :- ',emp2.name)\n", |
| 164 | + "print('Employee ID :- ',emp2.empid)\n", |
| 165 | + "emp2.greet()" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "code", |
| 170 | + "execution_count": 7, |
| 171 | + "id": "24d83028", |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [ |
| 174 | + { |
| 175 | + "data": { |
| 176 | + "text/plain": [ |
| 177 | + "'India'" |
| 178 | + ] |
| 179 | + }, |
| 180 | + "execution_count": 7, |
| 181 | + "metadata": {}, |
| 182 | + "output_type": "execute_result" |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "emp2.country = 'India' #instance variable can be created manually\n", |
| 187 | + "emp2.country" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "id": "7b8759b8", |
| 193 | + "metadata": {}, |
| 194 | + "source": [ |
| 195 | + "# Inheritance\n", |
| 196 | + "- Inheritance is a powerful feature in object oriented programming.\n", |
| 197 | + "- Inheritance provides code reusability in the program because we can use an existing class\n", |
| 198 | + "- (Super Class/ Parent Class / Base Class) to create a new class (Sub Class / Child Class /Derived Class) \n", |
| 199 | + " instead of creating it from scratch.\n", |
| 200 | + "- The child class inherits data definitions and methods from the parent class which facilitates the reuse of features already available. The child class can add few more definitions or redefine a base class method.\n", |
| 201 | + "- Inheritance comes into picture when a new class possesses the 'IS A' relationship with an existing class. \n", |
| 202 | + "- E.g Student is a person. Hence person is the base class and student is derived class" |
| 203 | + ] |
| 204 | + }, |
| 205 | + { |
| 206 | + "cell_type": "code", |
| 207 | + "execution_count": 13, |
| 208 | + "id": "ae97e23d", |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [ |
| 211 | + { |
| 212 | + "name": "stdout", |
| 213 | + "output_type": "stream", |
| 214 | + "text": [ |
| 215 | + "Student Details\n", |
| 216 | + "---------------\n", |
| 217 | + "Name :- Sunil\n", |
| 218 | + "Age :- 40\n", |
| 219 | + "Gender :- Male\n", |
| 220 | + "Student ID :- 12345\n", |
| 221 | + "Fees :- 25000\n", |
| 222 | + "\n", |
| 223 | + "Employee Details\n", |
| 224 | + "---------------\n", |
| 225 | + "Name :- Albert\n", |
| 226 | + "Age :- 45\n", |
| 227 | + "Gender :- Male\n", |
| 228 | + "Employee ID :- 6789\n", |
| 229 | + "Salary :- 75000\n" |
| 230 | + ] |
| 231 | + } |
| 232 | + ], |
| 233 | + "source": [ |
| 234 | + "class person: # Parent Class\n", |
| 235 | + " def __init__(self, name , age , gender):\n", |
| 236 | + " self.name = name\n", |
| 237 | + " self.age = age\n", |
| 238 | + " self.gender = gender\n", |
| 239 | + " def PersonInfo(self):\n", |
| 240 | + " print('Name :- {}'.format(self.name))\n", |
| 241 | + " print('Age :- {}'.format(self.age))\n", |
| 242 | + " print('Gender :- {}'.format(self.gender))\n", |
| 243 | + "\n", |
| 244 | + "class student(person): # Child Class\n", |
| 245 | + " def __init__(self,name,age,gender,studentid,fees):\n", |
| 246 | + " person.__init__(self,name,age,gender)\n", |
| 247 | + " self.studentid = studentid\n", |
| 248 | + " self.fees = fees\n", |
| 249 | + " \n", |
| 250 | + " def StudentInfo(self):\n", |
| 251 | + " print('Student ID :- {}'.format(self.studentid))\n", |
| 252 | + " print('Fees :- {}'.format(self.fees))\n", |
| 253 | + "\n", |
| 254 | + "class teacher(person): # Child Class\n", |
| 255 | + " def __init__(self,name,age,gender,empid,salary):\n", |
| 256 | + " person.__init__(self,name,age,gender)\n", |
| 257 | + " self.empid = empid\n", |
| 258 | + " self.salary = salary\n", |
| 259 | + " def TeacherInfo(self):\n", |
| 260 | + " print('Employee ID :- {}'.format(self.empid))\n", |
| 261 | + " print('Salary :- {}'.format(self.salary))\n", |
| 262 | + "\n", |
| 263 | + "stud1 = student('Sunil' , 40 , 'Male' , 12345 , 25000)\n", |
| 264 | + "print('Student Details')\n", |
| 265 | + "print('---------------')\n", |
| 266 | + "stud1.PersonInfo() # PersonInfo() method presnt in Parent Class will be access\n", |
| 267 | + "stud1.StudentInfo()\n", |
| 268 | + "print()\n", |
| 269 | + "teacher1 = teacher('Albert' , 45 , 'Male' , 6789 , 75000)\n", |
| 270 | + "print('Employee Details')\n", |
| 271 | + "print('---------------')\n", |
| 272 | + "teacher1.PersonInfo() # PersonInfo() method presnt in Parent Class will be acc\n", |
| 273 | + "teacher1.TeacherInfo()" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "code", |
| 278 | + "execution_count": 14, |
| 279 | + "id": "558f40ae", |
| 280 | + "metadata": {}, |
| 281 | + "outputs": [ |
| 282 | + { |
| 283 | + "name": "stdout", |
| 284 | + "output_type": "stream", |
| 285 | + "text": [ |
| 286 | + "Contractual Employee Details\n", |
| 287 | + "****************************\n" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "ename": "NameError", |
| 292 | + "evalue": "name 'contractual' is not defined", |
| 293 | + "output_type": "error", |
| 294 | + "traceback": [ |
| 295 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 296 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 297 | + "Cell \u001b[1;32mIn[14], line 28\u001b[0m\n\u001b[0;32m 26\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mContractual Employee Details\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 27\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m****************************\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m---> 28\u001b[0m contract1 \u001b[38;5;241m=\u001b[39m \u001b[43mcontractual\u001b[49m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mDilip\u001b[39m\u001b[38;5;124m'\u001b[39m , \u001b[38;5;241m36\u001b[39m , \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMale\u001b[39m\u001b[38;5;124m'\u001b[39m , \u001b[38;5;241m4507\u001b[39m , \u001b[38;5;241m65000\u001b[39m,\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m21-12-2024\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 29\u001b[0m contract1\u001b[38;5;241m.\u001b[39mPersonInfo()\n\u001b[0;32m 30\u001b[0m contract1\u001b[38;5;241m.\u001b[39memployeeInfo()\n", |
| 298 | + "\u001b[1;31mNameError\u001b[0m: name 'contractual' is not defined" |
| 299 | + ] |
| 300 | + } |
| 301 | + ], |
| 302 | + "source": [ |
| 303 | + "class person: # Parent Class\n", |
| 304 | + " def __init__(self, name , age , gender):\n", |
| 305 | + " self.name = name\n", |
| 306 | + " self.age = age\n", |
| 307 | + " self.gender = gender\n", |
| 308 | + " def PersonInfo(self):\n", |
| 309 | + " print('Name :- {}'.format(self.name))\n", |
| 310 | + " print('Age :- {}'.format(self.age))\n", |
| 311 | + " print('Gender :- {}'.format(self.gender))\n", |
| 312 | + "\n", |
| 313 | + "class employee(person): # Child Class\n", |
| 314 | + " def __init__(self,name,age,gender,empid,salary):\n", |
| 315 | + " person.__init__(self,name,age,gender)\n", |
| 316 | + " self.empid = empid\n", |
| 317 | + " self.salary = salary\n", |
| 318 | + " def employeeInfo(self):\n", |
| 319 | + " print('Employee ID :- {}'.format(self.empid))\n", |
| 320 | + " print('Salary :- {}'.format(self.salary))\n", |
| 321 | + "\n", |
| 322 | + "class fulltime(employee): # Grand Child Class\n", |
| 323 | + " def __init__(self,name,age,gender,empid,salary,WorkExperience):\n", |
| 324 | + " employee.__init__(self,name,age,gender,empid,salary)\n", |
| 325 | + " self.WorkExperience = WorkExperience\n", |
| 326 | + " def FulltimeInfo(self):\n", |
| 327 | + " print('Work Experience :- {}'.format(self.WorkExperience))\n", |
| 328 | + "print('Contractual Employee Details')\n", |
| 329 | + "print('****************************')\n", |
| 330 | + "contract1 = contractual('Dilip' , 36 , 'Male' , 4507 , 65000,'21-12-2024')\n", |
| 331 | + "contract1.PersonInfo()\n", |
| 332 | + "contract1.employeeInfo()\n", |
| 333 | + "contract1.ContractInfo()\n", |
| 334 | + "print('\\n \\n')\n", |
| 335 | + "\n", |
| 336 | + "rint('Fulltime Employee Details')\n", |
| 337 | + "print('****************************')\n", |
| 338 | + "fulltim1= fulltime('Sunil' , 40 , 'Male' , 7021 , 75000, 12)\n", |
| 339 | + "fulltim1.PersonInfo()\n", |
| 340 | + "fulltim1.employeeInfo()\n", |
| 341 | + "fulltim1.FulltimeInfo()" |
| 342 | + ] |
| 343 | + } |
| 344 | + ], |
| 345 | + "metadata": { |
| 346 | + "kernelspec": { |
| 347 | + "display_name": "Python 3 (ipykernel)", |
| 348 | + "language": "python", |
| 349 | + "name": "python3" |
| 350 | + }, |
| 351 | + "language_info": { |
| 352 | + "codemirror_mode": { |
| 353 | + "name": "ipython", |
| 354 | + "version": 3 |
| 355 | + }, |
| 356 | + "file_extension": ".py", |
| 357 | + "mimetype": "text/x-python", |
| 358 | + "name": "python", |
| 359 | + "nbconvert_exporter": "python", |
| 360 | + "pygments_lexer": "ipython3", |
| 361 | + "version": "3.9.12" |
| 362 | + } |
| 363 | + }, |
| 364 | + "nbformat": 4, |
| 365 | + "nbformat_minor": 5 |
| 366 | +} |
0 commit comments