0% found this document useful (0 votes)
1 views34 pages

Inheritance_python

This chapter is helpefull for student who are seeking to learn python inheritance and all the topic are disscused

Uploaded by

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

Inheritance_python

This chapter is helpefull for student who are seeking to learn python inheritance and all the topic are disscused

Uploaded by

fall3210060
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Inheritance

Inheritance:
 A programming technique that is used to reuse an
existing class to built new class is known as inheritance.

 The new class inherits all the behavior of the original


class.

 The existing class that reused to create a new class is


known as super class, base class or parent class.

 The new class that inherits the properties and functions


of existing class is known as subclass, derived class or
child class.
Conti….
 The inheritance relationship between the classes of a
program is known as class hierarchy.

 The basic principle of inheritance is that each subclass


share common properties with the class from which it is
derived.

 The child class inherits all the capabilities of the parent


class and can add its own capabilities.
Example
 Suppose a class name vehicle is already created.

 The subclass of this class may share common properties


such as wheels and motor etc.

 Additionally a subclass may have its own characteristics for


example ,a subclass BUS may have seats for people but
another subclass TRUCK may space for goods.
Conti….

Vehicle

Bus Truck Motorcycle


Advantages of Inheritance
 Reusability :
Inheritance allows to developer to reuse existing code in many
situations. A class can be created once and it can be reused again
and again.

 Saves time and effort :


Inheritance saves a lot of time and effort to write the same
classes again.

 Increases Program structure and reliability :


A super class is already compiled and test properly. This class can
be used in a new application without compiling it again. The use
of existing class increase program reliability.
Protected Access Specifier
 The private data members of a class are only accessible in the
class in which they are declared.
 The public data members are accessible from anywhere in the
program.
 The protected access specifier is different from private and
public specifiers.
 It is specially used in inheritance.
 It allows a protected data members to be accessed from all
derived classes but not from anywhere else in the program.
 It means that child class can access all protected data
members of its parent class
The difference between private, public and protected access
specifiers
Categories of Inheritance

 Single Inheritance:
A type of inheritance in which a child class is derived
from a single parent class is known as single inheritance.

Parent
Vehicle
class

Child Class
Single Inheritance Or Simple
Inheritance
 Syntax
Example
Conti…
 Multiple inheritance:
A type of inheritance in which a child class is derived
from multiple parent classes is known as multiple
inheritance.
Multiple Inheritance
 Syntax
Example
Multilevel Inheritance
 In multilevel inheritance, a child class becomes a parent class
for another child class, which accesses all the properties and
methods of both classes.

 In other words, each class inherits attributes and methods from


its immediate parent class, which in turn inherit from another
parent class.

 Python does not restrict the number of levels at which we will


get multilevel inheritance. We can do inheritance (i.e. parent-
child relationships) on any number of levels. It is not bound
with only three levels, as shown in the below figure.
Conti…
 The difference between a single level inheritance and
multilevel inheritance is that single inheritance involves
a class inheriting from a single parent class, while
multilevel inheritance involves a chain of inheritance
with each class inheriting from its immediate parent
class.
Syntax
Conti…
 In this syntax:
Class1 is the top-level class.
Class2 inherits from Class1, creating a parent-child
relationship.
Class3 inherits from Class2, creating a multilevel
inheritance chain.
You can continue this pattern for as many classes (Class4,
Class5, and so on) as needed, with each class inheriting
from the one immediately preceding it.

 This syntax correctly establishes a multilevel


inheritance hierarchy in Python, where each child class
inherits the attributes and methods of its immediate
parent class in the chain.
Example
Example 2
Hierarchical Inheritance

 Hierarchical Inheritance is a type of inheritance in which a single


base class is inherited by multiple derived classes.
 In this scenario, each derived class shares common attributes
and methods from the same base class, forming a hierarchy of
classes.
Syntax
Conti…
 In the above syntax, ParentClass is the name of the parent
class, containing attributes and methods that we want to inherit
in the child classes.

 ChildClass1 and ChildClass2 are the names of the child classes


that inherit attributes and methods from the ParentClass. Each
child class can have its own attributes and methods in addition
to those inherited from the parent class.

 With hierarchical inheritance, multiple child classes can inherit


the properties and methods from the same parent class. This
allows us to reuse code and build a family of classes that share
common properties while having their unique properties.
Example
Conti…
Example 2
Hybrid Inheritance:
 Hybrid inheritance in Python is a combination of
hierarchical inheritance and multiple inheritance within a
single program or class hierarchy. It allows us to create
complex class structures that can serve various purposes.
 From the figure, it is clear that above the half portion
represents hierarchical inheritance and below the half
portion represents multiple inheritance. This relationship
represents hybrid inheritance.
Syntax
Conti..
 In this simple syntax in previous silde, ParentClass1 is
the superclass of both child classes ChildClass1 and
ChildClass2.

 This shows the hierarchical inheritance.

 ChildClass3 demonstrates multiple inheritance, inheriting


from both ChildClass1 and ChildClass2.

 This is the basic syntax showing the concept of hybrid


inheritance.
Super ( )
Conti…
Conti….

You might also like