Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fd7d66d

Browse files
OOPs Polymorphism
1 parent 462428f commit fd7d66d

File tree

1 file changed

+223
-0
lines changed

1 file changed

+223
-0
lines changed

‎Polymorphism/Day1.ipynb‎

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Polymorphism"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"#### Many Form"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": null,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"## Method Overriding"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"###### Method overriding allows a chlid class to provide a specific implementation of a method that is already define in its parent class "
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 8,
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"name": "stdout",
40+
"output_type": "stream",
41+
"text": [
42+
"Woof!-Woof!\n",
43+
"Meow!-Meow!\n",
44+
"Woof!-Woof!\n"
45+
]
46+
}
47+
],
48+
"source": [
49+
"## Base class\n",
50+
"class Animal:\n",
51+
" def speak(self):\n",
52+
" return \"Sound of animal\"\n",
53+
" \n",
54+
"## Derived Class 1 \n",
55+
"class Dog(Animal):\n",
56+
" def speak(self):\n",
57+
" return \"Woof!-Woof!\"\n",
58+
"\n",
59+
"## Derived Class 2\n",
60+
"class Cat(Animal):\n",
61+
" def speak(self):\n",
62+
" return \"Meow!-Meow!\" \n",
63+
"\n",
64+
"## Function For Polymorphism Demonstration\n",
65+
"def animal_speak(animal): # this animal is obj of class\n",
66+
" print(animal.speak())\n",
67+
"\n",
68+
"dog = Dog() \n",
69+
"cat = Cat()\n",
70+
"print(dog.speak())\n",
71+
"print(cat.speak())\n",
72+
"animal_speak(dog)"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": 11,
78+
"metadata": {},
79+
"outputs": [
80+
{
81+
"name": "stdout",
82+
"output_type": "stream",
83+
"text": [
84+
"The area is 20\n",
85+
"The area is 78.5\n"
86+
]
87+
}
88+
],
89+
"source": [
90+
"## Polymorphism with Functions And Methods\n",
91+
"\n",
92+
"class Shape: ##BASE CLASS\n",
93+
" def area(self):\n",
94+
" return \"Area of The Figure \"\n",
95+
" \n",
96+
"## Derived Class 1\n",
97+
"class Rectangle(Shape):\n",
98+
" def __init__(self,width,height):\n",
99+
" self.width = width\n",
100+
" self.height = height\n",
101+
"\n",
102+
" def area(self):\n",
103+
" return self.width * self.height\n",
104+
"## Derived Class 2 \n",
105+
"class Circle(Shape):\n",
106+
" def __init__(self,radius):\n",
107+
" self.radius = radius\n",
108+
" def area(self):\n",
109+
" return 3.14 * self.radius *self.radius\n",
110+
"\n",
111+
"#Function that demonstrate Polymorphism\n",
112+
"\n",
113+
"def print_area(shape): #parameter but intended to be obj of class\n",
114+
" print(f\"The area is {shape.area()}\")\n",
115+
"\n",
116+
"rectangle = Rectangle(4,5)\n",
117+
"circle = Circle(5)\n",
118+
"\n",
119+
"print_area(rectangle)\n",
120+
"print_area(circle)"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"##### Interface - Abstract base class"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": 12,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"#### Polymorphism with Abstract Base Class"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
142+
"source": [
143+
"##### Abstract Base classes (ABSs) are used to define common methods for a group of related objects they can enforce that derived classes implement particular methods, promoting consistency across different implementations"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 13,
149+
"metadata": {},
150+
"outputs": [
151+
{
152+
"name": "stdout",
153+
"output_type": "stream",
154+
"text": [
155+
"Car Engine Started\n"
156+
]
157+
}
158+
],
159+
"source": [
160+
"from abc import ABC, abstractmethod\n",
161+
"\n",
162+
"## Define an abstract class\n",
163+
"class Vehicle(ABC):\n",
164+
" @abstractmethod\n",
165+
" def start_engine(self):\n",
166+
" pass\n",
167+
"## Derived Class 1\n",
168+
"class Car(Vehicle):\n",
169+
" def start_engine(self):\n",
170+
" return \"Car Engine Started\"\n",
171+
" \n",
172+
"## Derived Class 2\n",
173+
"class Motorcycle(Vehicle):\n",
174+
" def start_engine(self):\n",
175+
" return \"Motorcycle engine started\"\n",
176+
" \n",
177+
"# Function That demonstrate Polymorphism\n",
178+
"def start_vehicle(vehicle):\n",
179+
" print(vehicle.start_engine())\n",
180+
"\n",
181+
"# create objects of Car and Motor cycle\n",
182+
"car = Car()\n",
183+
"Motorcycle = Motorcycle()\n",
184+
"start_vehicle(car)\n"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {},
191+
"outputs": [],
192+
"source": []
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": null,
197+
"metadata": {},
198+
"outputs": [],
199+
"source": []
200+
}
201+
],
202+
"metadata": {
203+
"kernelspec": {
204+
"display_name": "base",
205+
"language": "python",
206+
"name": "python3"
207+
},
208+
"language_info": {
209+
"codemirror_mode": {
210+
"name": "ipython",
211+
"version": 3
212+
},
213+
"file_extension": ".py",
214+
"mimetype": "text/x-python",
215+
"name": "python",
216+
"nbconvert_exporter": "python",
217+
"pygments_lexer": "ipython3",
218+
"version": "3.11.7"
219+
}
220+
},
221+
"nbformat": 4,
222+
"nbformat_minor": 2
223+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /