Course Content

  • Inheritance

Course Content

Python Inheritance

Hello guys. Welcome to LearnVern. In the last topic we saw Class & Objects.

We saw how to create a class and work with objects.

Today we will learn about Inheritance.

What is Inheritance? How does it work & how is it used?

We will also see the theoretical & practical parts.

First of all, what is Inheritance?

Inheritance basically is using one class’s property into another one.

The concept here is Super Class & Base Class.

Other names are Parent Class & Child Class.

Or they can also be called Derived Class & Base Class.

Suppose you made a method in some class & you want to reuse that method in a 2nd class, you can do that by inheriting the property from the above class to the below class.

You can use the Parent class’s property into the Child class.

Inheritance means taking a property of one class into another class.

There are certain types of Inheritance.

Single Inheritance. Multilevel Inheritance. Multiple Inheritance.

Hierarchical Inheritance. And Hybrid Inheritance.

These are the 5 types of Inheritance available in Python.

Let’s begin with knowing what Single Inheritance is.

In Single Inheritance, there is only 1 Parent Class & 1 Child Class in which the inheritance takes place.

We will work one by one with all types of Inheritance.

Moving onto practicals.

Our Jupyter is open. Let’s see Single Inheritance.

I’ll write here the title Single Inheritance.

I already told you that in Single Inheritance, there is 1 Parent Class & 1 Student Class & both use eachother’s properties.

I’ll make a class here which will be called Parent Class.

In which I made a function Def myfunction1.

As it is a class, it is compulsory to write Self.

I’ll write now Parent Class Function Called.

I’ll make another class here which is Child Class.

I’m making a Child Class to make it clearer for you.

And make 1 more function which will be myfunction2. 

It is compulsory to write Self here as well.

And I wrote here Child Class Function Called.

Now it’s time for Inheritance.

It is very very simple in Python.

Here, you have to use the property of the Parent Class into the Child Class.

How will you do it?

You have to make brackets here & write the name of your Parent Class here.

Now an Object will be created here.

But initially there will be a confusion as to make which object’s class.

The rule of Inheritance depicts that you must create only and only Child Class Object.

Creating an Object of Child Class.

The object of Child Class C1 is ChildClass.

Now we have to use the Parent Class’s property from the Child Class.

I’ll write here Parent Class Property & this is my Parent Class.

In the same way, this is my Child Class which inherits the Parent Class.

This is Child Class Property.

Now we have to access the property of the Parent Class through Child Class Object.

We can already access Child Class Property through it.

If we write c1.myfunction1, we are calling the Parent Class Property here.

We made an object for Child Class but we could access the property of Parent Class as well which means our Parent Class is inherited in Child Class.

We called c1.myfunction2 as well, you can see that the Child Class Function got called as well.

Which eventually means that 1 class is getting inherited by another class.

So this is how Single Inheritance works.

Moving ahead to Multilevel Inheritance. There are levels of Class here.

There is a Parent class, then Sub-parent Class and then Child Class.

Now we shall go to the practicals to see how Multilevel Inheritance works.

I’ll make a new cell here in which I’ll markdown Multilevel Inheritance.

Now what does Multilevel Inheritance mean?

See the diagram...if you are able to understand the diagram, it means you already understand the practicals.
There is Parent Class, then Sub-parent Class and then Child Class.

Which further means that Parent Class is inherited in Sub-parent class which is further inherited in Child Class.

Let’s see how it’s made..

I’ll take class A. This is our Parent Class.

In which I made a function Def myfunction, in which I further wrote Self.

I’ll write here Class A function Called.

I made another class in the same way and called it B.

Which is a Sub-parent Class.

In which I again made a function myfunction2. Self.

I’ll print the Class B function called.

I’ll copy paste & edit B here.

Again in the same way I’ll make a 3rd Class which is

Class C, our Child Class.

Made another function myfunction3. Self.

And printed Class C function called.

Now how to Inherit?

There are levels of class here.

Class A will be inherited in Class B & class B will be inherited in Class C.

Class A will be inherited in Class B & class B will be inherited in Class C.

The object which will be made will be of C.

Why? Because that is our Child Class and all the properties will be present in C.

How? Since Class A is inherited in Class B, Class B already has the properties of Class A.

And B will be inherited in C so C ultimately has all the properties of A & B.

Let’s see how to access them.

I took an object here C1, class of C.

I’ll write it down here for you to understand.

Creating an object of class C. C1 is the object.

C1.myfunction. We had made this function for Class A.

You can see that class A got called.

In the same way, we will call myfunction2 which is our Class B.

Class B got called as well.

Next I’ll call the function of Child Class as well and it got called as well.

Here, we inherited Property of 1 class into 2nd & 2nd class property into 3rd.

The object will always be made of Child Class.

So we used the properties of Parent & Sub-parent with the help of 1 class.

This is how Multilevel Inheritance works under Python,

Our next Inheritance is Multiple Inheritance.

Before we talked about Multilevel Inheritance that had levels of class, now we will talk about Multiple Inheritance.

This happens when two Parent Class are inherited in 1 Child Class.

The diagram makes it way easier to understand, right?

Two Parent Class is being inherited into 1 Child Class.

Going over to practicals.

I’ll make another cell & markdown Multiple Inheritance.

Now here, how does Parent Class & Child Class work?

I made a Class A1 here. Which is our 1st Parent Class.

I made a function myfunction1. Remember to write Self.

I printed the Class A1 function Called.

In the same way, I took Class B1. Which is our 2nd Parent Class.

In which I wrote Def myfunction2. Self.

Print Class B1 Function Called.

I made a 3rd Class C1 in the similar manner, which is our Child Class.

Def myfunction3. Self. Print Class C1 Function Called.

Now how do we have to do the Inheritance process in the Child class?

You simply have to write A1 comma B1 in brackets.

You have to do multiple inheritance in this manner.

Here, both the Parent Properties arrived in Child Class.

Now…

Creating an object of Class C1.

Took C1 to create a Class C1 object.

We can access the properties of all the classes.

See function 2 is also called as well as function 3.

We will run it & you can see all the 3 functions of the class got called.

So this is how Multiple Inheritance works.

I hope you understood the Inheritances we learnt until now.

Moving on to Hierarchical Inheritance.

Here, there is only 1 Parent through which there are multiple Child Classes.

Going to practicals.

I’ll mark down Hierarchical Inheritance.

Here there is 1 parent class & multiple child class.

I’ll make a Class A1 here which will be our Parent Class.

In which I made a function myfunction1. Self.

Print Class A1 Function Called.

I’ll make a Class A2 here which is our Child Class.

Made a function in here myfunction2. Self.

Print Class A2 Function Called.

In the same manner, I called Class A3 here which is our 2nd Child Class.

 Myfunction3. Self.

Print Class A3 Function Called.

Here I have 2 child classes and 1 Parent Class.

Now how does the Hierarchical Inheritance work here?

All you got to do is inherit Class A1 in both the child classes.

2 objects will be made here.

Creating 2 objects. 1 for Class A2 and 1 for Class A3.

First of all we will create A2.

A2. and A3.

Here are my 2 objects.

You can access the properties of Class A1 or of that particular class.

A2.myfunction1. Function of Class A1.

A2 will also call the function of that class in particular as well.

Function of Class A2.

Run & we can access the property of A1 with the help of A2 object.

 Now with the help of A3, we will call Class A1.

A3 and the function will surely work in that class.

We haven’t added the round brackets here so it showed that error.

You can see that the A1 function is called with the A3 function too.

So this is how Hierarchical Inheritance works.

Now we will see Hybrid Inheritance.

Over here, there is a combination of Multiple Inheritance.

Combinations like 1 Parent Class, multiple Child Class or 2 Child Class can be Parent as well… such combinations are there in Inheritance.

Finally going to Practicals.

There’s no need to get confused if there are more than 1 type of combinations that is why the name Hybrid.

Hybrid Inheritance.

I’ll make a class here. Class B1. Parent Class.

I’ll make a function in it def myfunction1. Self.

Print Function of Class B1.

I’ll copy paste this to make 4 more classes.

This will be B2, this one B3 and B4.

This is Child Class, this too will be Child Class, this one will be Child Class as well.

This function will be B2, this B3 and this B4.

What is the diagram of Hybrid illustrating?

There are 2 Child Class from 1 Parent Class and from those 2 Child Class, there is another Child Class.

We will work according to the diagram.

B1 will be inherited in B2 & in B3.

Then B2 & B3 will be inherited in B4.

We can make an object of anything.

If we want an object of 2 Classes, we can take B2 & B3.

If we want to access the properties of all classes, we will make an object of only & only B4.

Creating an object of class B4. I took the object of B1 for B4 class.

Object of Class B4.

B1.myfunction 1..

Now we have to change the function names.

Myfunction4, myfunction3 & myfunction2.

I have to make the B4 object.

Why B4? Because I have inherited B1 into B2 & B3.

And further inherited B2 & B3 in B4.

So B4 is our immediate Child Class.

I’ll write here Creating an Object of Class B4.

I’ll make an object of B4.

Object of B4 Class.

Now I can call all the methods through B4 Object.

Function 2 & function 3 got called as well.

Function 4 too got called.

When I run it, you can see all the 4 functions appeared.

So what did we see in Inheritance today?

We saw how Inheritance works in Python. We also saw its types.

Such as Single, Multilevel, Multiple, Hierarchical & Hybrid Inheritance.

In the next video we will learn about Polymorphism.

See you in the next video.

Thank you.

FAQs

Inheritance is described as a class's capacity to derive or inherit properties from another class and use them whenever they are needed.

Inheritance in Python allows for code reuse, readability, and scalability. Inheritance in Python eliminates code repetition. The parent class can have all of the usual methods and properties. The child class that is derived from it has access to these.

Every object-oriented programming language has to support inheritance. This means Python allows inheritance, and it's one of the few languages that supports multiple inheritance, as you'll see later.

Inheritance allows us to create a class that inherits all of another class's methods and properties. The parent class, often known as the base class, is the one from which the inherited class is derived. A child class, also known as a derived class, is a class that inherits from another class.

In Python, like C++, a class can be derived from multiple base classes. Multiple inheritance is the term for this. Multiple inheritance follows the same syntax as single inheritance.

Share With Friend

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

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • 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