Hello guys. Welcome to LearnVern.
In the last topic we saw Polymorphism in which we saw 2 types of Polymorphism.
1 was Compile Time Polymorphism & other was Runtime Polymorphism.
Further in Compile Time, we saw Method Overloading & in Runtime we saw Method Overriding.
We also saw how to use Polymorphism in Python in the last topic.
We will move towards Abstraction where we will see how Abstraction works in Python and we will also see the theory & practicals.
First of all we will see what Abstraction is.
The meaning is very very simple.
Abstraction is used to hide a particular data.
All the methods that we are going to use, won’t have bodies because we implement that method into another class.
And then those classes use the Abstract method in their own way.
For eg: the Reserve Bank. The Reserve Bank makes a certain method which all the banks under it follow according to their system.
So here the concept is Parent & Child Concept.
The Parent class is called Abstract Class within which you make an Abstract method which doesn’t have any body.
And you have to compulsory implement it in your child class in which your Abstract class is inherited.
So this is what Abstraction means.
Let’s see how to create an Abstract Method & Class.
There is one Abstract class & one Abstract method.
First you have to create an Abstract class and an Abstract Method.
And in the body statement you just have to write Pass.
Why? Because the other Child Classes will decide its body.
All the Abstract methods in Abstract class won’t have any bodies.
Understood how Abstract method & Abstract class works?
Moving onto practicals.
I’ll open Jupyter for practicals.
First of all, how to create an Abstract Class?
We will see both how to create Abstract Class & Abstract Method.
I’ll take the same example here which I took in theory.
The Reserve Bank one.
I took a class here named RBI.
In which I made an Abstract Method. An Abstract Method.
I made a method named Def Interest. I wrote Self inside.
And then I passed it.
An Abstract class & an Abstract method is made.
I’ll write a comment here to make it easier for you.
Abstract Class & this is the Abstract Method.
Now as we all know, there are multiple banks in India which come under RBI.
I’ll create some more classes in which we will inherit our Abstract class that is RBI.
And then we will use the interest method in it.
I’ll take a class here called SBI.
Now I have to compulsory inherit the Abstract class in SBI.
Why? Because RBI has an Abstract method which I have to use in SBI Class.
How will we do it?
We have already read the concept of Inheritance in our Inheritance video.
Those who have not seen, kindly watch it first & then continue this Abstraction video.
First of all we have to inherit the RBI class into the SBI Class.
Bracket open RBI.
I will now use the interest method of RBI here.
I’ll write down the same Interest method where I wrote Self.
SBI gave its body. Print SBI Interest is 5%.
Here, SBI used the interest method in its own way.
I’ll make another class in the same way.
Class HDFC. Inherited RBI in the same way here too.
Because I want to use the RBI Interest method here too.
How will I do it? Def Interest. Self.
Print HDFC Interest is 2%.
So what is happening here?
The RBI bank class made an Interest Method which is our Abstract Method.
And the RBI Class has not given any body to that method.
The RBI Interest Method is implemented here.
We have implemented the RBI Interest method here.
We have used Hierarchical Inheritance here.
We have learned about Hierarchical Inheritance previously in the Inheritance video.
There was 1 Parent class & multiple Child Classes.
Those of you who don’t know the practicals, kindly check the Inheritance video to make it simpler for you in Abstraction.
We have 2 child classes here.
This is our Child Class. And this is our Child Class as well.
How many objects will be made?
2 objects. 1 will be of SBI class & another will be HDFC Class.
We will make an object here. I’ll comment here Creating an object of Class SBI.
Let’s make an object of SBI first.
2nd object. Creating an object of class HDFC.
We have 2 objects here.
The concept is very easy. That our RBI Class is our Abstract class which has made an Abstract method & it has not given any body to that method.
We used that method in the remaining Child Class where those Child Classes gave the Abstract method, a body according to itself.
Now we will write S . (dot) Interest. As soon as I call the Interest method, the SBI method will be called.
The one which we implemented.
SBI Interest Method Called.
I’ll also call the method through the HDFC object.
I’ll write here the HDFC Interest Method called.
We called both the methods here & as soon as we ran it, we got the output.
SBI Interest is 5% & HDFC Interest is 2%.
And who made the Interest method? The RBI Class.
And who implemented & used them? The Child Classes did.
I hope you are understanding the concept here.
I’ll give you 1 more example which will be easier for you to understand.
We will see another example of Abstraction.
I’ll take 1 more cell & make another class here, Animal.
This class is Abstract class. You can also call it Parent Class.
I made a method here called Move.
This method is the Abstract method.
It is an Abstract method so can we give it a body? No we cannot.
So we will write Pass.
We will make another class Dog.
What is the rule of Abstraction? We have to inherit from the Parent that is our Abstract class.
Bracket open. Inherit the Animal Class.
We will use the Move, Abstract method we made in Animal.
I’ll write Self here.
This is our Implement Method.
Class Animal Implement method here.
We implemented the Class Animal method here.
Now we will give a body to our Abstract method that is Move, according to this class
Let’s see how.
Print I can bark.
I’ll make a class in the same way here, class Snake.
I inherited Animal in it as well.
I’ll again implement the Move method of Animal class. Self.
Print I can hisss.
Finally we have a Parent class & 2 Child Class.
Same concept as the one we used in RBI, only the example is different.
So that you can understand it better.
I have 2 Child Classes here so I’ll make 2 objects.
Creating an object of Class Dog.
I took the object name D here & initialised a class Dog.
I’ll create another object here in the same way.
Creating an Object of Class Snake.
I took the object name S here & initialised a class Snake.
Now I will call the Move method with the help of these 2 objects.
See. As soon as I run it, both of the methods got called by the Child Class object.
So this is how Abstraction works in Python.
In the next video we will see Encapsulation.
So see you in the next video.
Have a friend to whom you would want to share this course?