0% found this document useful (0 votes)
31 views7 pages

Types of Inheritance and Polymorphism in Python

Inheritance in python

Uploaded by

tgariya32
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)
31 views7 pages

Types of Inheritance and Polymorphism in Python

Inheritance in python

Uploaded by

tgariya32
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

2 6 8 9 14 16 20 26 29 35 38 43 48 57 68 74 77 84 95 97 98 100

Inheritance allows a class (called a child or derived class) to use the properties and methods
of another class (called a parent or base class).

Syntax:

class Parent:
# parent members

class Child(Parent):
# child members

1. Single Inheritance
A child class inherits from only one parent class.

Example:
class Parent:
def display_parent(self):
print("This is the Parent class")

class Child(Parent):
def display_child(self):
print("This is the Child class")

# Create object
obj = Child()
obj.display_parent()
obj.display_child()

Output:
This is the Parent class
This is the Child class

✅ Here, Child inherits the properties of Parent.

2. Multiple Inheritance
A child class inherits from more than one parent class.

Example:
class Father:
def father_info(self):
print("Father: Hardworking and disciplined")

class Mother:
def mother_info(self):
print("Mother: Caring and supportive")

class Child(Father, Mother):


def child_info(self):
print("Child: Inherits traits from both parents")

obj = Child()
obj.father_info()
obj.mother_info()
obj.child_info()

Output:
Father: Hardworking and disciplined
Mother: Caring and supportive
Child: Inherits traits from both parents

✅ The Child class inherits from both Father and Mother.

3. Multilevel Inheritance
Inheritance occurs in a chain (a class inherits from another derived class).

Example:
class Grandfather:
def show_grandfather(self):
print("I am the Grandfather")

class Father(Grandfather):
def show_father(self):
print("I am the Father")

class Child(Father):
def show_child(self):
print("I am the Child")

obj = Child()
obj.show_grandfather()
obj.show_father()
obj.show_child()

Output:
I am the Grandfather
I am the Father
I am the Child

✅ Inheritance flows from Grandfather → Father → Child.

4. Hierarchical Inheritance
Multiple child classes inherit from a single parent class.

Example:
class Parent:
def display_parent(self):
print("This is the Parent class")

class Child1(Parent):
def display_child1(self):
print("This is Child 1")

class Child2(Parent):
def display_child2(self):
print("This is Child 2")

obj1 = Child1()
obj2 = Child2()

obj1.display_parent()
obj2.display_parent()

Output:
This is the Parent class
This is the Parent class

✅ Both Child1 and Child2 share the same Parent class.

5. Hybrid Inheritance
A combination of two or more types of inheritance.

Example:
class A:
def showA(self):
print("Class A")

class B(A):
def showB(self):
print("Class B")

class C(A):
def showC(self):
print("Class C")

class D(B, C): # Hybrid (Combination of Multiple + Hierarchical)


def showD(self):
print("Class D")

obj = D()
[Link]()
[Link]()
[Link]()
[Link]()

Output:
Class A
Class B
Class C
Class D

✅ Here, D inherits from both B and C, which themselves inherit from A — forming a hybrid
structure.

B section 27 37 44 54 61 63 103 104 106


Bca 2 5 14 20 28 30 36 41 47 62 65 66 67 75 79 90 98 110 116 118

Type of Inheritance Description Example Classes


Single One parent, one child A → B
Multiple One child, multiple parents A, B → C
Multilevel Chain of inheritance A → B → C
Hierarchical One parent, multiple children A → B, A → C
Hybrid Combination of types A → B, C → D

What is Polymorphism?
The word Polymorphism means "many forms".
In programming, it refers to the ability of different objects to respond to the same function or
method call in different ways.

In simple words:

The same function name can be used for different types (classes), and each type can behave
differently when that function is called.
🔹 Example 1: Polymorphism with Built-in Functions
Many Python functions show polymorphism naturally.
For example, the len() function works with different data types:

print(len("Python")) # String → counts characters


print(len([10, 20, 30])) # List → counts elements
print(len({1: 'A', 2: 'B'})) # Dictionary → counts keys

Output:
6
3
2

✅ The same function len() behaves differently depending on the object type.

Example 2: Polymorphism with Methods


We can define methods with the same name in different classes, and each method can have its
own behavior.

class Bird:
def fly(self):
print("Most birds can fly.")

class Penguin:
def fly(self):
print("Penguins cannot fly.")

# Create objects
obj1 = Bird()
obj2 = Penguin()

# Same method name, different behaviors


for animal in (obj1, obj2):
[Link]()

Output:
Most birds can fly.
Penguins cannot fly.

✅ Both classes have a method fly(), but their outputs differ based on the object type.
Example 3: Polymorphism with Inheritance (Method
Overriding)
Polymorphism often appears through method overriding, where a child class redefines a
method from its parent class.

class Vehicle:
def start(self):
print("Starting the vehicle...")

class Car(Vehicle):
def start(self):
print("Starting the car with key...")

class Bike(Vehicle):
def start(self):
print("Starting the bike with self-start...")

# Objects
v = Vehicle()
c = Car()
b = Bike()

for obj in (v, c, b):


[Link]()

Output:
Starting the vehicle...
Starting the car with key...
Starting the bike with self-start...

✅ The same method start() behaves differently for each subclass.

Example 4: Polymorphism with Abstract Classes


(Advanced)
Polymorphism can also be enforced using abstract classes (from the abc module).

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass
class Circle(Shape):
def area(self):
print("Area = π × r²")

class Rectangle(Shape):
def area(self):
print("Area = length × breadth")

# Objects
shapes = [Circle(), Rectangle()]
for shape in shapes:
[Link]()

Output:
Area = π × r²
Area = length × breadth

✅ All shapes must define their own version of the area() method — polymorphism ensures
flexibility.

Concept Description Example


One interface, multiple Same method name in different
Definition
implementations classes
Built-in Example len(), max(), sum() Work with strings, lists, tuples
User-defined
fly() in Bird and Penguin Different outputs
Example
Inheritance-based Method overriding start() in Car and Bike
Abstract Class Force subclasses to override methods area() in Circle, Rectangle

You might also like