Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Types of Inheritance in Python

 

In this lecture we will see that,

What is inheritance ?

Which are the different types of inheritance ?

How to implement it ?

 

When we have different types of classes and we want to use a particular class's method in a different class, then we use inheritance.

 

Python supports inheritance, it even supports multiple inheritance.

Classes can inherit from other classes.

A class can inherit attributes and behaviour methods from another class, called the superclass.

 

Syntax :

class BaseClassName:

Statements

class DerivedClassName(BaseClassName):

Statements

 

Multilevel inheritance

 

Syntax :

class A:

Statements

class B(A):

Statements

class C(B):

Statements

 

Hierarchical inheritance

 

Syntax :

class A:

Statements

class B(A):

Statements

class C(A):

Statements

 

Multiple inheritance

 

Syntax :

class A:

Statements

class B:

Statements

class C(A,B):

Statements

 

Hybrid inheritance

 

Syntax :

class A:

Statements

class B(A):

Statements

class C(A):

Statements

class D(B,C):

Statements

 

Example for Simple inheritance :

class A:

def Adata(self):

print("Method of A")

class B(A):

def Bdata(self):

print("Method of B")

ob=B()

ob.Bdata()

ob.Adata()

O/P :

Method of B

Method of A

 

Example for Multilevel inheritance :

class A:

def Adata(self):

print("Method of A")

class B(A):

def Bdata(self):

print("Method of B")

class C(B):

def Cdata(self):

print("Method of C")

ob=C()

ob.Cdata()

ob.Bdata()

ob.Adata()

O/P :

Method of C

Method of B

Method of A

 

Example for Hierarchical inheritance :

class A:

def Adata(self):

print("Method of A")

class B(A):

def Bdata(self):

print("Method of B")

class C(A):

def Cdata(self):

print("Method of C")

ob=C()

ob.Cdata()

ob.Adata()

O/P :

Method of C

Method of A

 

Example for Multiple inheritance :

class A:

def Adata(self):

print("Method of A")

class B:

def Bdata(self):

print("Method of B")

class C(A,B):

def Cdata(self):

print("Method of C")

ob=C()

ob.Cdata()

ob.Bdata()

ob.Adata()

O/P :

Method of C

Method of B

Method of A

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere