0% found this document useful (0 votes)
3 views3 pages

Complex Inheritance in Python Examples

The document contains 50 complex inheritance programs in Python, showcasing various inheritance techniques such as diamond problem, abstract base classes, multiple inheritance, and polymorphism. Each example demonstrates different aspects of object-oriented programming, including method overriding, mixins, and hierarchical structures. The programs illustrate practical applications in scenarios like hospital management, banking, and e-commerce.

Uploaded by

shubhamgoud002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Complex Inheritance in Python Examples

The document contains 50 complex inheritance programs in Python, showcasing various inheritance techniques such as diamond problem, abstract base classes, multiple inheritance, and polymorphism. Each example demonstrates different aspects of object-oriented programming, including method overriding, mixins, and hierarchical structures. The programs illustrate practical applications in scenarios like hospital management, banking, and e-commerce.

Uploaded by

shubhamgoud002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

50 Complex Inheritance Programs in Python

1. Diamond Problem with super()


class A:
def show(self):
print("Class A")

class B(A):
def show(self):
super().show()
print("Class B")

class C(A):
def show(self):
super().show()
print("Class C")

class D(B, C):


def show(self):
super().show()
print("Class D")

d = D()
[Link]()

2. Abstract Base Class Example


from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Circle(Shape):
def __init__(self, r):
self.r = r
def area(self):
return 3.14 * self.r * self.r

print("Circle Area:", Circle(5).area())

3. Multiple Inheritance (Role Combination)


class Flyer:
def action(self):
print("Can fly")

class Swimmer:
def action(self):
print("Can swim")

class Duck(Flyer, Swimmer):


def action(self):
super().action()
print("Duck can do both")

d = Duck()
[Link]()

4. Hospital Management (Multilevel)


class Person:
def __init__(self, name):
[Link] = name
class Doctor(Person):
def duty(self):
print([Link], "treats patients")

class Surgeon(Doctor):
def duty(self):
super().duty()
print([Link], "performs surgery")

s = Surgeon("Dr. Mehta")
[Link]()

5. Bank Account with Polymorphism


class Account:
def withdraw(self, amt):
print("General withdrawal:", amt)

class Savings(Account):
def withdraw(self, amt):
print("Savings withdrawal:", amt, "with limited access")

class Current(Account):
def withdraw(self, amt):
print("Current withdrawal:", amt, "with overdraft allowed")

for acc in [Savings(), Current()]:


[Link](5000)

6. Using super() in Multiple Inheritance


class A:
def __init__(self):
print("Init A")

class B(A):
def __init__(self):
super().__init__()
print("Init B")

class C(A):
def __init__(self):
super().__init__()
print("Init C")

class D(B, C):


def __init__(self):
super().__init__()
print("Init D")

D()

7. Mixin Example
class JsonMixin:
def to_json(self):
import json
return [Link](self.__dict__)

class Person(JsonMixin):
def __init__(self, name, age):
[Link], [Link] = name, age

p = Person("Shubham", 21)
print(p.to_json())

8. Hierarchical + Polymorphism
class Vehicle:
def speed(self):
print("General speed")

class Car(Vehicle):
def speed(self):
print("Car speed: 120 km/h")

class Bike(Vehicle):
def speed(self):
print("Bike speed: 80 km/h")

for v in [Car(), Bike()]:


[Link]()

9. Interface-like Abstract Class


from abc import ABC, abstractmethod

class Database(ABC):
@abstractmethod
def connect(self): pass

class MySQL(Database):
def connect(self):
print("Connected to MySQL")

class MongoDB(Database):
def connect(self):
print("Connected to MongoDB")

for db in [MySQL(), MongoDB()]:


[Link]()

10. Hybrid Example (E-Commerce)


class Product:
def info(self):
print("Product Info")

class Electronics(Product):
def category(self):
print("Electronics")

class Clothing(Product):
def category(self):
print("Clothing")

class SmartWatch(Electronics, Clothing):


def category(self):
super().category()
print("SmartWatch: Electronics + Fashion")

sw = SmartWatch()
[Link]()
[Link]()

You might also like