1.
Single Inheritance
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
d = Dog()
d.speak()
d.bark()
------------------------------------------------------------------
class Vehicle:
def move(self):
print("Vehicle moves")
class Car(Vehicle):
def horn(self):
print("Car horn")
c = Car()
c.move()
c.horn()
2. Multiple Inheritance
class Father:
def skills(self):
print("Father: Gardening")
class Mother:
def skills(self):
print("Mother: Cooking")
class Child(Father, Mother):
def own_skills(self):
print("Child: Painting")
c = Child()
c.skills() # Father’s skill due to method resolution order
c.own_skills()
--------------------------------------------------------------------
class Writer:
def write(self):
print("Writes stories")
class Artist:
def draw(self):
print("Draws paintings")
class Person(Writer, Artist):
pass
p = Person()
p.write()
p.draw()
3. Multilevel Inheritance
class Animal:
def eat(self):
print("Eats food")
class Mammal(Animal):
def walk(self):
print("Walks on land")
class Dog(Mammal):
def bark(self):
print("Barks")
d = Dog()
d.eat()
d.walk()
d.bark()
----------------------------------------------------------------------
class ElectronicDevice:
def power(self):
print("Needs power")
class Computer(ElectronicDevice):
def compute(self):
print("Computes data")
class Laptop(Computer):
def portable(self):
print("Easy to carry")
l = Laptop()
l.power()
l.compute()
l.portable()
4. Hierarchical Inheritance
class Animal:
def eat(self):
print("Eats food")
class Dog(Animal):
def bark(self):
print("Dog barks")
class Cat(Animal):
def meow(self):
print("Cat meows")
dog = Dog()
cat = Cat()
dog.eat()
cat.eat()
dog.bark()
cat.meow()
--------------------------------------------------------
class Vehicle:
def move(self):
print("Vehicle is moving")
class Bike(Vehicle):
def ride(self):
print("Riding bike")
class Car(Vehicle):
def drive(self):
print("Driving car")
b = Bike()
c = Car()
b.move()
c.move()
b.ride()
c.drive()
5. Hybrid Inheritance
class A:
def feature1(self):
print("Feature 1")
class B(A):
def feature2(self):
print("Feature 2")
class C:
def feature3(self):
print("Feature 3")
class D(B, C):
def feature4(self):
print("Feature 4")
d = D()
d.feature1()
d.feature2()
d.feature3()
d.feature4()
----------------------------------------------------------------
class School:
def location(self):
print("Located in city")
class Teacher(School):
def teach(self):
print("Teaches subjects")
class Staff(School)s:
def manage(self):
print("Manages school")
class Principal(Teacher, Staff): # Hybrid: Multilevel + Multiple
def lead(self):
print("Leads school")
p = Principal()
p.location()
p.teach()
p.manage()
p.lead()