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 ()
738obj .reddy ()
74-
75- # Method from Tirumala (child class)
76- obj .tirumala ()
9+ obj .kondareddy ()
0 commit comments