0% found this document useful (0 votes)
4 views13 pages

Understanding Inheritance in Python

Inheritance allows one class to derive properties from another, facilitating code reuse and concept reuse. In Python, classes can inherit from a parent class without special keywords, and there are various types of inheritance including single, multiple, multi-level, and hybrid. Public and private attributes can be defined, with public attributes accessible across derived classes and private attributes using a double underscore.

Uploaded by

ViShesh
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)
4 views13 pages

Understanding Inheritance in Python

Inheritance allows one class to derive properties from another, facilitating code reuse and concept reuse. In Python, classes can inherit from a parent class without special keywords, and there are various types of inheritance including single, multiple, multi-level, and hybrid. Public and private attributes can be defined, with public attributes accessible across derived classes and private attributes using a double underscore.

Uploaded by

ViShesh
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

Inheritance

Introduction
• In inheritance, one class is derived from another class, and the
properties of the parent class can be used by the derived class.
• Inheritance is important because it allows the programmers to create
new classes, which specify a new implementation while maintaining
the same behaviours of the old class.
• Inheritance is the process of deriving a class from another existing
class.
• Inheritance facilitates the reusability of the code and extends the
original properties of the class without modifying it.
Motivations for using inheritance
• 1. code reuse. Because a child class can inherit behaviour from a
parent class, the code does not need to be rewritten for the child.
• 2. Concept reuse: This occurs when a child’s class overrides behaviour
defined by the parent.
In deriving a new class from a class, two classes are defined as parent
class and child class.
Parent class: A parent class is a class from which a class is derived. The
parent class is also called super class.
Child Class: A child class is a class that is derived from another class and
is also called derived class. It is also called sub class.
Inheritance in Python
• In python, there are no special keywords while deriving a class from a
base class.
• For deriving the child class from a parent class, the name of the
parent class should be given as a parameter while defining the child
class.
• The syntax for inheriting a class from a class is as follows:
Inheritance and Types of Attributes
• There are two attributes, namely private and public.
• The public attributes are accessible across all the derived classes.
• The private attributed are defined and using the “__” double
underscore operator.
• Types of Inheritance
1. Single Inheritance
2. Multiple Inheritance
3. Multi-level inheritance
4. Hybrid Inheritance
Single Inheritance
• When a derived class inherits its properties from one base class, it is
called single inheritance.
• The Derived class possesses the characteristics of the base class and
also its properties. The syntax for the single inheritance is as follows:
Illustration for single inheritance by deriving
class Apple from the base class Eatable_fruits
Multiple Inheritance
• Multiple inheritance refers to the type of inheritance where the
derived class possesses the characteristics of one or more base
classes.
• The derived class has all the features of base classes along with the
new featured that are defined in the derived class. The Syntax as
follows:
Illustration for multiple inheritance by creating a class TF representing Teaching faculty and another
class student and then derive a class called TA from both the class TF and Student
Multi-Level Inheritance
• Multi-level inheritance is the type of inheritance in which is derived
from another derived class. The syntax for multi-level inheritance as
follows:
Illustrating the multi-level inheritance by deriving a class Students from the class user and another
class ClassActivities from the class Students.
Hybrid Inheritance
• Deriving a class from the derived classes which are derived from a
same base class is called hybrid inheritance. The syntax for the hybrid
inheritance as follows:
Illustration for hybrid inheritance by creating classes University, Course< Branch and Student using
hybrid inheritance

You might also like