Skip to content

Commit 6025407

Browse files
inheritance_super_demo
1 parent 3090567 commit 6025407

2 files changed

Lines changed: 188 additions & 16 deletions

File tree

OOPS/super_function_example.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# -------------------------------------------------------------
2+
# PYTHON INHERITANCE EXAMPLE WITH super()
3+
# Author: Learning Example
4+
#
5+
# This file demonstrates:
6+
# 1. Constructor inheritance using super()
7+
# 2. Method overriding using super()
8+
# 3. Common mistakes beginners make and how to fix them
9+
# -------------------------------------------------------------
10+
11+
12+
# =============================================================
13+
# EXAMPLE 1: USING super() WITH CONSTRUCTORS (__init__)
14+
# =============================================================
15+
16+
# Parent Class
17+
class konda:
18+
# Constructor to initialize the name
19+
def __init__(self, name):
20+
self.name = name
21+
22+
23+
# Child Class inheriting from konda
24+
class amba(konda):
25+
26+
# Child constructor
27+
def __init__(self, name, age):
28+
29+
# super() calls the parent class constructor
30+
# This initializes the variable "name"
31+
super().__init__(name)
32+
33+
# New attribute specific to child class
34+
self.age = age
35+
36+
37+
# Creating object of child class
38+
emp = amba("kondareddy", 20)
39+
40+
# Accessing inherited and child attributes
41+
print(emp.name)
42+
print(emp.age)
43+
44+
45+
# -------------------------------------------------------------
46+
# EXPECTED OUTPUT
47+
# kondareddy
48+
# 20
49+
# -------------------------------------------------------------
50+
51+
52+
53+
# =============================================================
54+
# EXAMPLE 2: METHOD OVERRIDING WITH super()
55+
# =============================================================
56+
57+
# Parent class
58+
class konda:
59+
# Method to store name
60+
def reddy(self, name):
61+
self.name = name
62+
63+
64+
# Child class inheriting from konda
65+
class amba(konda):
66+
67+
# Method overriding
68+
# This method has the same name as the parent method
69+
def reddy(self, name, age):
70+
71+
# super() calls the parent class method
72+
# This sets the "name"
73+
super().reddy(name)
74+
75+
# Child class adds a new attribute
76+
self.age = age
77+
78+
79+
# Creating object
80+
emp = amba()
81+
82+
# Calling overridden method
83+
emp.reddy("kondareddy", 20)
84+
85+
# Printing values
86+
print(emp.name)
87+
print(emp.age)
88+
89+
90+
# -------------------------------------------------------------
91+
# EXPECTED OUTPUT
92+
# kondareddy
93+
# 20
94+
# -------------------------------------------------------------
95+
96+
97+
98+
# =============================================================
99+
# MISTAKES I MADE WHILE LEARNING
100+
# =============================================================
101+
102+
# ❌ Mistake 1: Passing parameters while creating object
103+
# when class does not have __init__
104+
105+
# WRONG
106+
# emp = amba("kondareddy",20)
107+
108+
# WHY?
109+
# Because Python automatically calls __init__ when an object
110+
# is created. If __init__ is not defined, Python's default
111+
# constructor does not accept arguments.
112+
113+
# ✔ FIX
114+
# emp = amba()
115+
# emp.reddy("kondareddy",20)
116+
117+
118+
119+
# -------------------------------------------------------------
120+
121+
# ❌ Mistake 2: Calling method without object
122+
123+
# WRONG
124+
# emp = reddy("kondareddy",20)
125+
126+
# WHY?
127+
# reddy() is a class method, not a global function.
128+
129+
# ✔ FIX
130+
# emp.reddy("kondareddy",20)
131+
132+
133+
134+
# -------------------------------------------------------------
135+
136+
# ❌ Mistake 3: Using super() when parent has no method
137+
138+
# Example WRONG usage
139+
# super().__init__(name)
140+
141+
# If parent class does not define __init__,
142+
# Python will raise an error.
143+
144+
# ✔ FIX
145+
# Ensure parent method exists before calling super()
146+
147+
148+
149+
# =============================================================
150+
# COMMON BEGINNER MISTAKES WITH super() AND INHERITANCE
151+
# =============================================================
152+
153+
# 1️⃣ Forgetting to call parent constructor
154+
# This results in missing attributes.
155+
156+
# 2️⃣ Passing wrong number of parameters to super()
157+
158+
# 3️⃣ Not understanding that super() follows
159+
# Python's Method Resolution Order (MRO)
160+
161+
# Example to check MRO:
162+
# print(amba.mro())
163+
164+
165+
166+
# =============================================================
167+
# KEY CONCEPTS DEMONSTRATED
168+
# =============================================================
169+
170+
# 1. Inheritance
171+
# Child class inherits properties from parent class.
172+
173+
# 2. Constructor chaining using super()
174+
175+
# 3. Method overriding
176+
# Child method replaces parent method but can still call it.
177+
178+
# 4. Code reuse
179+
# super() allows reuse of parent functionality.
180+
181+
182+
183+
# =============================================================
184+
# SIMPLE RULE TO REMEMBER
185+
# =============================================================
186+
187+
# super() → used to call parent class methods
188+
# especially constructors (__init__)

OOPS/tempCodeRunnerFile.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)