Skip to content

Commit 95f0d35

Browse files
committed
add override and polymorphism
1 parent f1c34ff commit 95f0d35

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Methods Override.
2+
# An override allows a subclass (child) method to provide its implementation
3+
# over a method (that has the same name) that is already defined in its
4+
# superclass (parent).
5+
# Let's take an example:
6+
class Parent:
7+
8+
def to_overide(self):
9+
return "This method is inside the Parent class"
10+
11+
12+
class Child(Parent):
13+
14+
def to_overide(self):
15+
return "This method is inside the Child class"
16+
17+
18+
# Create an instance of the Child class
19+
object_sub = Child()
20+
# print(object_sub.to_overide())
21+
22+
23+
# Overriding can be used in:
24+
# 1.Multiple Inheritance
25+
class Parent1:
26+
27+
def to_overide(self):
28+
return "This method is inside the Parent1 class"
29+
30+
31+
class Parent2:
32+
33+
def to_overide(self):
34+
return "This method is inside the Parent2 class"
35+
36+
37+
class ChildRev(Parent1, Parent2):
38+
39+
def to_overide(self):
40+
return "Inside the Child class"
41+
42+
43+
# Create an instance of the Child class
44+
obj_sub = ChildRev()
45+
# print(obj_sub.to_overide())
46+
47+
48+
# 2.Multilevel Inheritance:
49+
class ParentRev:
50+
51+
def to_overide(self):
52+
return "Inside the ParentRev class"
53+
54+
55+
class Child1(ParentRev):
56+
57+
def to_overide(self):
58+
return "Inside the Child1 class"
59+
60+
61+
class GrandChild(Child1):
62+
63+
def to_overide(self):
64+
return "Inside the GrandChild class"
65+
66+
67+
sub_child = Child1()
68+
# print(sub_child.to_overide())
69+
70+
grand_child = GrandChild()
71+
# print(grand_child.to_overide())
72+
73+
74+
# Where and why do we use method overriding?
75+
# 1. Maintainability: When you use method overriding, you can change the
76+
# behavior of methods in a subclass without altering the superclass code.
77+
78+
# 2. Polymorphism.
79+
# Method overriding enables polymorphism, allowing different objects to respond
80+
# differently to the same method call. Different versions of the methods are
81+
# called based on the object type.
82+
# Polymorphism will be discussed in the next section.
83+
84+
# 3. Overriding is used in abstraction, which will be discussed later in this
85+
# section.
86+
87+
# 4. The above points lead to code flexibility.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# The 4 Pillars of OOP in Python:
2+
# 2. Polymorphism
3+
# "Polymorphism" means "many forms", and in programming when a methods,
4+
# or functions, etc., have the same name and can be executed on many objects of
5+
# the different or same type.
6+
# Let's take an example:
7+
# The len() function, can be executed on different data types:
8+
# Lists
9+
# print(len([2, 5, 3, 1]))
10+
11+
# Strings
12+
# print(len("Hello"))
13+
14+
# Dictionary
15+
# print(len({"g": 1, "h": 2, "y": 4, "u": 5, "N": 7}))
16+
17+
18+
# Class Polymorphism
19+
# Polymorphism is used with class methods, where there is a method that has
20+
# the same name in multiple classes.
21+
# Let's take an example:
22+
class Dogs:
23+
24+
def __init__(self, name, age):
25+
self.name = name
26+
self.age = age
27+
28+
def sound(self):
29+
return "Heard my dog barking."
30+
31+
32+
class Cats:
33+
34+
def __init__(self, name, age):
35+
self.name = name
36+
self.age = age
37+
38+
def sound(self):
39+
return "Heard my cat meowing."
40+
41+
42+
class Roosters:
43+
44+
def __init__(self, name, age):
45+
self.name = name
46+
self.age = age
47+
48+
def sound(self):
49+
return "Heard my rooster crowing."
50+
51+
52+
# Create classes instances:
53+
dog = Dogs("leo", 6)
54+
cat = Cats("Oreo", 2)
55+
rooster = Roosters("RuRu", 1)
56+
57+
# To call the sound method for all the objects, we the "for" loop:
58+
# for i in [dog, cat, rooster]:
59+
# print(i.sound())
60+
61+
# Because of Polymorphism we can excute the sound() method for the three
62+
# classes.
63+
64+
65+
# Polymorphism with Inheritance:
66+
class Animals:
67+
def __init__(self, name, age):
68+
self.name = name
69+
self.age = age
70+
71+
def sound(self):
72+
return f"{self.name} voice"
73+
74+
75+
class Dog(Animals):
76+
77+
# Reuse and override the sound method in the Animals class in the Dog
78+
# subclass
79+
def sound(self):
80+
return f"{super().sound()} is barking."
81+
82+
83+
class Cat(Animals):
84+
85+
# Override the sound method in the Animal class and inherit the Animal
86+
# attributes.
87+
def sound(self):
88+
return f"{self.name} is meowing."
89+
90+
91+
class Rooster(Animals):
92+
93+
# Override the sound method in the Animal class and inherit the Animal
94+
# attributes.
95+
def sound(self):
96+
return f"{self.name} is crowing."
97+
98+
99+
dog = Dog("leo", 6)
100+
cat = Cat("Oreo", 2)
101+
rooster = Rooster("RuRu", 1)
102+
names = ["dog", "cat", "rooster"]
103+
animals_obj = [dog, cat, rooster]
104+
105+
for i in range(len(names)):
106+
print(f"My {names[i]} {animals_obj[i].name} age is {animals_obj[i].age} \
107+
years old")
108+
print(animals_obj[i].sound())

0 commit comments

Comments
 (0)