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

Python Inheritance Types Explained

The document explains different types of inheritance in Python: single, multiple, multilevel, hierarchical, and hybrid. Each type is illustrated with code examples and explanations of how classes inherit methods from parent classes. Outputs for each example demonstrate the functionality of the inherited methods.

Uploaded by

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

Python Inheritance Types Explained

The document explains different types of inheritance in Python: single, multiple, multilevel, hierarchical, and hybrid. Each type is illustrated with code examples and explanations of how classes inherit methods from parent classes. Outputs for each example demonstrate the functionality of the inherited methods.

Uploaded by

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

INHERITANCE IN PYTHON

1. Single Inheritance
✅ Code:
python
Copy code
class Animal:
def speak(self):
print("Animal speaks")

class Dog(Animal):
def bark(self):
print("Dog barks")

d = Dog()
[Link]()
[Link]()
🧠 Explanation:

 Animal is the parent class with method speak().


 Dog is the child class, which inherits from Animal.
 Dog can use both its own method (bark) and the inherited method (speak).
 Output:

Animal speaks
Dog barks

🔹 2. Multiple Inheritance
✅ Code:
python
Copy code
class Flyable:
def fly(self):
print("Can fly")

class Swimmable:
def swim(self):
print("Can swim")

class Duck(Flyable, Swimmable):


pass

duck = Duck()
[Link]()
[Link]()
🧠 Explanation:

 Flyable and Swimmable are two independent classes.


 Duck inherits from both, gaining access to fly() and swim().
 Output:

Can fly
Can swim

🔹 3. Multilevel Inheritance
✅ Code:
python
Copy code
class Animal:
def eat(self):
print("Eats food")

class Bird(Animal):
def lay_eggs(self):
print("Lays eggs")

class Sparrow(Bird):
def chirp(self):
print("Chirps")

s = Sparrow()
[Link]()
s.lay_eggs()
[Link]()
🧠 Explanation:

 Animal → Bird → Sparrow


 Sparrow inherits from Bird, and Bird inherits from Animal.
 So Sparrow gets all methods from both parent and grandparent.
 Output:

Eats food
Lays eggs
Chirps

🔹 4. Hierarchical Inheritance
✅ Code:
python
Copy code
class Vehicle:
def start(self):
print("Vehicle started")
class Car(Vehicle):
def drive(self):
print("Car is driving")

class Bike(Vehicle):
def ride(self):
print("Bike is riding")

c = Car()
b = Bike()
[Link]()
[Link]()
🧠 Explanation:

 Car and Bike both inherit from the same parent class Vehicle.
 Both get the start() method from Vehicle.
 Output:

Vehicle started
Vehicle started

🔹 5. Hybrid Inheritance
✅ Code:
python
Copy code
class A:
def methodA(self):
print("Method from A")

class B(A):
def methodB(self):
print("Method from B")

class C:
def methodC(self):
print("Method from C")

class D(B, C):


def methodD(self):
print("Method from D")

d = D()
[Link]()
[Link]()
[Link]()
🧠 Explanation:

 D inherits from both B and C.


 B already inherits from A, so D indirectly gets methodA().
 This is a hybrid of multilevel + multiple inheritance.
 Output:

Method from A
Method from C
Method from D

You might also like