-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolymorphism.py
More file actions
39 lines (31 loc) · 771 Bytes
/
polymorphism.py
File metadata and controls
39 lines (31 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Parent class
class Animal:
def __init__(self, name):
self.name = name
# Generic sound method for any animal
def sound(self):
print("Making a sound")
# Child class Dog
class Dog(Animal):
def __init__(self, name, breed, age):
super().__init__(name)
self.breed = breed
self.age = age
# Overridden sound method for Dog
def sound(self):
print("Woof!")
# Child class Cat
class Cat(Animal):
def __init__(self, name, breed, age):
super().__init__(name)
self.breed = breed
self.age = age
# Overridden sound method for Cat
def sound(self):
print("Meow!")
# Creating instances
my_dog = Dog("Jax", "Bulldog", 5)
my_cat = Cat("Lily", "Ragdoll", 2)
animals = [my_dog, my_cat]
for animal in animals:
animal.sound()