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

Inheritance

The document explains the concept of inheritance in Python, detailing its types including single, multiple, multilevel, hierarchical, and hybrid inheritance. It describes classes and objects as fundamental components of object-oriented programming, emphasizing the reusability of code through inheritance. Examples are provided for each type of inheritance to illustrate their implementation in Python.

Uploaded by

suganyaa.aids
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 views11 pages

Inheritance

The document explains the concept of inheritance in Python, detailing its types including single, multiple, multilevel, hierarchical, and hybrid inheritance. It describes classes and objects as fundamental components of object-oriented programming, emphasizing the reusability of code through inheritance. Examples are provided for each type of inheritance to illustrate their implementation in Python.

Uploaded by

suganyaa.aids
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
You are on page 1/ 11

https://www.geeksforgeeks.

org/types-of-inheritance-python/

Python is a versatile programming language that supports various programming styles,


including object-oriented programming (OOP) through the use of objects and classes.

Class

A class is a blueprint for objects

A Class is a user-defined blueprint or prototype from which objects are created. It represents
the set of properties or methods that are common to all objects of one type. Using classes, you
can create multiple objects with the same behavior instead of writing their code multiple times.
This includes classes for objects occurring more than once in your code.

class Parrot:

# class attribute

name = ""

age = 0

Object

An Object is a basic unit of Object-Oriented Programming that represents real-life entities.

An object is any entity that has attributes and behaviors. For example, a parrot is an object. It
has

 attributes - name, age, color, etc.

 behavior - dancing, singing, etc.

1. State: It is represented by the attributes of an object. It also reflects the properties of an


object.

2. Behavior: It is represented by the methods of an object. It also reflects the response of


an object to other objects.

Example:

class Parrot:

# class attribute
name = ""

age = 0

# create parrot1 object

parrot1 = Parrot()

parrot1.name = "Blu"

parrot1.age = 10

# create another object parrot2

parrot2 = Parrot()

parrot2.name = "Woo"

parrot2.age = 15

# access attributes

print(f"{parrot1.name} is {parrot1.age} years old")

print(f"{parrot2.name} is {parrot2.age} years old")

Output

Blu is 10 years old

Woo is 15 years old

Inheritance (Parent Child Releationship

Inheritance is a way of creating a new class for using details of an existing class without
modifying it.

The newly formed class is a derived class (or child class). Similarly, the existing class is a
base class (or parent class).

Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that we want, we can derive our new
class from the existing class. By doing this, we are reusing the fields and methods of the existing
class.

The newly formed class is a derived class (or child class). Similarly, the existing class is a
base class (or parent class).

Types of Inheritance in Python


Types of Inheritance depend upon the number of child and parent classes involved. There are
four types of inheritance in Python:

Single Inheritance:

Single inheritance enables a derived class to inherit properties from a single parent class, thus
enabling code reusability and the addition of new features to existing code.

Example:

# Python program to demonstrate

# single inheritance

# Base class

class Parent:

def func1(self):

print("This function is in parent class.")

# Derived class

class Child(Parent):

def func2(self):

print("This function is in child class.")


# Driver's code

object = Child()

object.func1()

object.func2()

Output:

This function is in parent class.

This function is in child class.

Multiple Inheritance:

When a class can be derived from more than one base class this type of inheritance is called
multiple inheritances. In multiple inheritances, all the features of the base classes are inherited
into the derived class.

# Python program to demonstrate multiple inheritance

# Base Parent class1

class Mother:

mothername = ""
def mother(self):

print(self.mothername)

# Base class2

class Father:

fathername = ""

def father(self):

print(self.fathername)

# Derived class

class Son(Mother, Father):

def parents(self):

print("Father :", self.fathername)

print("Mother :", self.mothername)

# Driver's code

s1 = Son()

s1.fathername = "RAM"

s1.mothername = "SITA"

s1.parents()

Output:

Father : RAM

Mother : SITA

Multilevel Inheritance :
In multilevel inheritance, features of the base class and the derived class are further inherited
into the new derived class. This is similar to a relationship representing a child and a
grandfather.

Example:

# Python program to demonstrate multilevel inheritance

# Base class

class Grandfather:

def __init__(self, grandfathername):

self.grandfathername = grandfathername

# Intermediate class

class Father(Grandfather):

def __init__(self, fathername, grandfathername):

self.fathername = fathername
# invoking constructor of Grandfather class

Grandfather.__init__(self, grandfathername)

# Derived class

class Son(Father):

def __init__(self, sonname, fathername, grandfathername):

self.sonname = sonname

# invoking constructor of Father class

Father.__init__(self, fathername, grandfathername)

def print_name(self):

print('Grandfather name :', self.grandfathername)

print("Father name :", self.fathername)

print("Son name :", self.sonname)

# Driver code

s1 = Son('Prince', 'Rampal', 'Lal mani')

print(s1.grandfathername)

s1.print_name()

Output:

Lal mani

Grandfather name : Lal mani

Father name : Rampal

Son name : Prince

Hierarchical Inheritance:
When more than one derived class are created from a single base this type of inheritance is
called hierarchical inheritance. In this program, we have a parent (base) class and two child
(derived) classes.

# Python program to demonstrate

# Hierarchical inheritance Base class

class Parent:

def func1(self):

print("This function is in parent class.")

# Derived class1

class Child1(Parent):

def func2(self):

print("This function is in child 1.")

# Derivied class2

class Child2(Parent):

def func3(self):

print("This function is in child 2.")


# Driver's code

object1 = Child1()

object2 = Child2()

object1.func1()

object1.func2()

object2.func1()

object2.func3()

Output:

This function is in parent class.

This function is in child 1.

This function is in parent class.

This function is in child 2.

Hybrid Inheritance:

Inheritance consisting of multiple types of inheritance is called hybrid inheritance.

Example:
# Python program to demonstrate

# hybrid inheritance

class School:

def func1(self):

print("This function is in school.")

class Student1(School):

def func2(self):

print("This function is in student 1. ")

class Student2(School):

def func3(self):

print("This function is in student 2.")

class Student3(Student1, School):

def func4(self):

print("This function is in student 3.")

# Driver's code
object = Student3()

object.func1()

object.func2()

Output:

This function is in school.

This function is in student 1.

You might also like