0% found this document useful (0 votes)
5 views

Python Inheritance Example

Computer engineering

Uploaded by

Pranay Dhole
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)
5 views

Python Inheritance Example

Computer engineering

Uploaded by

Pranay Dhole
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
You are on page 1/ 6

Inheritance in Python

Inheritance in Python (Description):

Inheritance is a feature in object-oriented programming that allows a class (called a child or derived

class) to inherit properties

and behaviors (methods) from another class (called a parent or base class).

This promotes code reusability and hierarchical classification.

Types of Inheritance in Python:

1. Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

5. Hybrid Inheritance

Examples of Each Type:

1. Single Inheritance:

In single inheritance, a child class inherits from one parent class.

Example:

class Animal:

def speak(self):

print("Animals make sound")


class Dog(Animal):

def bark(self):

print("Dog barks")

d = Dog()

d.speak() # Inherited method

d.bark() # Own method

Output:

Animals make sound

Dog barks

2. Multiple Inheritance:

In multiple inheritance, a child class inherits from more than one parent class.

Example:

class Animal:

def speak(self):

print("Animals make sound")

class Pet:

def type_of_pet(self):

print("This is a pet")

class Dog(Animal, Pet):

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

d = Dog()

d.speak() # Inherited from Animal

d.type_of_pet() # Inherited from Pet

d.bark() # Own method

Output:

Animals make sound

This is a pet

Dog barks

3. Multilevel Inheritance:

In multilevel inheritance, a class is derived from another derived class.

Example:

class Animal:

def speak(self):

print("Animals make sound")

class Mammal(Animal):

def walk(self):

print("Mammals walk")

class Dog(Mammal):

def bark(self):

print("Dog barks")
d = Dog()

d.speak() # Inherited from Animal

d.walk() # Inherited from Mammal

d.bark() # Own method

Output:

Animals make sound

Mammals walk

Dog barks

4. Hierarchical Inheritance:

In hierarchical inheritance, multiple classes inherit from a single parent class.

Example:

class Animal:

def speak(self):

print("Animals make sound")

class Dog(Animal):

def bark(self):

print("Dog barks")

class Cat(Animal):

def meow(self):

print("Cat meows")
d = Dog()

d.speak() # Inherited from Animal

d.bark() # Own method

c = Cat()

c.speak() # Inherited from Animal

c.meow() # Own method

Output:

Animals make sound

Dog barks

Animals make sound

Cat meows

5. Hybrid Inheritance:

In hybrid inheritance, two or more types of inheritance are combined.

Example:

class Animal:

def speak(self):

print("Animals make sound")

class Pet:

def type_of_pet(self):

print("This is a pet")

class Mammal(Animal):
def walk(self):

print("Mammals walk")

class Dog(Mammal, Pet):

def bark(self):

print("Dog barks")

d = Dog()

d.speak() # Inherited from Animal

d.walk() # Inherited from Mammal

d.type_of_pet() # Inherited from Pet

d.bark() # Own method

Output:

Animals make sound

Mammals walk

This is a pet

Dog barks

Summary:

- Single Inheritance: One class inherits from another.

- Multiple Inheritance: One class inherits from multiple classes.

- Multilevel Inheritance: A class inherits from a derived class, forming a chain.

- Hierarchical Inheritance: Multiple classes inherit from a single parent class.

- Hybrid Inheritance: A combination of two or more types of inheritance.

You might also like