Skip to content

Commit 7b558c8

Browse files
Sinlge_inheritence_code
1 parent 324f007 commit 7b558c8

2 files changed

Lines changed: 41 additions & 74 deletions

File tree

OOPS/single_inheritence.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -----------------------------------------
2+
# Example Program: Single Inheritance
3+
# -----------------------------------------
4+
5+
# Base Class (Parent Class)
6+
class konda:
7+
8+
# Method defined inside the parent class
9+
def reddy(self):
10+
# This method prints a message
11+
print("Kondareddy")
12+
13+
14+
# Derived Class (Child Class)
15+
# This class inherits from the parent class 'konda'
16+
class DerivedClass(konda):
17+
18+
# Method defined inside the child class
19+
def kondareddy(self):
20+
# This method prints a message
21+
print("Ambavaram")
22+
23+
24+
# Creating an object of the child class
25+
# The object can access methods from both the parent and child classes
26+
obj = DerivedClass()
27+
28+
# Calling method from the parent class
29+
# Even though the method is defined in 'konda',
30+
# it can be used because DerivedClass inherits from it
31+
obj.reddy()
32+
33+
# Calling method from the child class
34+
obj.kondareddy()

OOPS/tempCodeRunnerFile.py

Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,9 @@
1-
# -------------------------------------------------------
2-
# Example Program: Demonstrating Multiple and Multilevel
3-
# Inheritance in Python
4-
# -------------------------------------------------------
5-
6-
# -------------------------------
7-
# Base Class (Level 1)
8-
# -------------------------------
9-
class Konda:
10-
# Method inside the base class
11-
def kondaa(self):
12-
# This method prints a message
13-
print("This method belongs to class Konda")
14-
15-
16-
# -------------------------------
17-
# Derived Class (Level 2)
18-
# This inherits from Konda
19-
# → This begins Multilevel Inheritance
20-
# -------------------------------
21-
class Ambavaram(Konda):
22-
# Method defined in this class
23-
def ambavaram(self):
24-
# This method prints a message
25-
print("This method belongs to class Ambavaram")
26-
27-
28-
# -------------------------------
29-
# Another Independent Parent Class
30-
# This class is not part of the chain
31-
# -------------------------------
32-
class Reddy:
33-
# Method defined in Reddy class
1+
class konda:
342
def reddy(self):
35-
# Prints a message
36-
print("This method belongs to class Reddy")
37-
38-
39-
# -------------------------------
40-
# Final Child Class
41-
# This class inherits from TWO classes:
42-
# 1. Ambavaram
43-
# 2. Reddy
44-
#
45-
# → Ambavaram already inherits from Konda
46-
# → So this forms BOTH:
47-
# Multilevel + Multiple Inheritance
48-
# -------------------------------
49-
class Tirumala(Ambavaram, Reddy):
50-
51-
# Method inside the final child class
52-
def tirumala(self):
53-
# Prints a message
54-
print("This method belongs to class Tirumala")
55-
56-
57-
# -------------------------------
58-
# Creating an object of Tirumala
59-
# -------------------------------
60-
obj = Tirumala()
61-
62-
# -------------------------------
63-
# Calling methods from all classes
64-
# -------------------------------
65-
66-
# Method from Konda (grandparent class)
67-
obj.kondaa()
68-
69-
# Method from Ambavaram (parent class)
70-
obj.ambavaram()
71-
72-
# Method from Reddy (second parent class)
3+
print("Kondareddy")
4+
class DerivedClass(Konda):
5+
def kondareddy(self):
6+
print("Ambavaram")
7+
obj=DerivedClass()
738
obj.reddy()
74-
75-
# Method from Tirumala (child class)
76-
obj.tirumala()
9+
obj.kondareddy()

0 commit comments

Comments
 (0)