[no audio]
Friends here we are discussing about
inheritance and polymorphism concept
of OOPS. First
let me go with polymorphism.
See basically we know 'poly' means many.
So in Python polymorphism allows us to define same methods
in different classes.
I mean, you can take same method name in different classes.
Even though if you define same method name in different classes,
there is no overlap, and this process is also known as method
overriding. Simply we are overwriting same method in
different classes. Yes, that is possible, that is allowed in
your Python. And we can also use this polymorphism concept in your
inheritance as well.
We will see that while discussing about inheritance. First
let me explain about your polymorphism with a simple
simple example. Let me open my Sublime Text Editor.
So here I am defining two classes,
let's say 'tomcat',
[no audio]
and I am defining a constructor method here.
The purpose of constructor method is to assign, let's say
'tomcat', 'home' and 'version' by default whenever we are creating
an object. We know initialization method is a constructor method.
It will execute by default whenever if you create an object.
Right. So simply I am writing 'home',
then let's say 'self.version = ver'.
Right. Then finally write,
I'm not expecting any 'return' value from this method,
that's why simply I am writing 'return None'. Then let me write
one more method called 'display'.
[no audio]
So simply I am writing,
"This is from tomcat class", and here
simply I am displaying 'print(
self.home), then 'print(
self.version)'.
That's it.
Then from this function
also, I mean from this method also I'm not expecting any
return value. Simply I'm writing 'return None'.
Let me take same method.
[no audio]
But here I am taking web server class name,
let's say 'Apache'.
[no audio]
Yes, we created two classes.
But if you observe, here I have taken initialization method,
and here also I have taken initialization method. 'display' method
common in both the classes.
No problem. It is allowed in your Python.
It is allowed in your Python.
[no audio]
Right. See if I run this code you're not getting any error.
Anyway, let me create an object from your 'tomcat',
and from your 'Apache'. Let's say 'tom_ob
= Tomcat()'.
So while creating an object you're having initialization
method which is expecting two arguments,
let's say 'home' and 'version'. Then let me pass that. Lets say
'/home/tomcat.
[no audio]
9', and then I am taking suppose 'version' as some 7.6,
something like that.
Then let me create 'apa_ob' also
from your 'Apache' class.
So it is also using initialization method, that means it is
expecting two arguments,
we need to pass them while creating an object.
Let's say 'version' as some 2.4. Let me run this and see the
result. There is no error.
That's fine.
Now I want to display the details of 'tom_ob', and also
display the details of 'apa_ob'. Now in 'tom_ob'
we have 'display' method. If I call this 'display' method
with respect to 'tom_ob'
then it will call from here only so that you are going
to get information for your tomcat object. See guys
simply we define same method name in different classes. No
problem. Even though if we take your Python don't get confused,
it will work perfectly. See the result.
[no audio]
This is from 'Tomcat' class.
[no audio]
Right.
Let me call from 'apa_ob', 'display' method.
Now what you are going to get, see the result. "This is from"
[no audio]
Yeah, sorry. I have written, actually I copied, right,
let me modify here 'apache'. Both the places
actually we have same code. Now,
let me run this and see the result.
Whenever if you are calling
from 'tom_ob', 'display' method it is going to execute your
this 'display' method, right. And whenever if you're calling
from 'apa_ob' it is executing the method which
is there in 'apa_ob'. That's it.
Even though if you override your method in different classes,
your Python won't get confused, and this is simply polymorphism.
Defining same method name in different classes is nothing
but polymorphism, right?
That's fine. Now our next concept is inheritance.
See inheritance is a mechanism that allows us to create new
class known as a child class, and that is based upon an existing
class. That is parent class.
Nothing is there. This concept is very similar to our
[no audio]
like human life. Suppose
we are inheriting genes from our parents.
Right. That is the concept here.
Let me explain this with a script so that you can understand.
Now, let me take one more script.
So here I am saving it as a 'inheritance.py'.
[no audio]
So guys first thing let me take same script only,
[no audio]
and if I open this script here.
Guys, if you observe in these two classes, there are common
methods, at least 'display' method.
[no audio]
See, for 'display' method already it is there in 'Tomcat'.
Why should I write a code here? If you observe entire syntax
is same no, here and here. If it is similar
why should I write same code in the second class?
[no audio]
Right. So in that case, what I can do is I can inherit 'Tomcat' class
in my 'Apache' so that I can use 'display' method for 'apa_ob',
for 'Apache' class. How it is possible? Just observe
that. I am removing 'display' method from your second class.
[no audio]
And if you want to inherit any class, instead of this object
you have to write the particular
class name. That's it.
Now the meaning for this is, you are inheriting these two
methods in this class.
And if you observe same method is there here and here. No
problem. Even though if you are having, first preference is
in this Apache, this one. If it is not there in Apache, then
only it will take this initialization method. That is called
polymorphism. That's fine.
Now see, let me create your two objects.
[no audio]
Right. Now remove this 'print' because you are inheriting this
'Tomcat' into this 'Apache' class.
Now, I created two objects.
It's fine. There is no error. Right. Now
see I am calling your 'tom_ob.display'.
[no audio]
Then you are getting 'Tomcat' details.
If you observe in 'Apache' we don't have, we don't have 'display'
method, but I am trying to do that. 'apa_ob.display'.
[no audio]
You're getting your information. Your 'display' method is executing
even though if you don't have 'display' method here because
we inherited this 'Tomcat' class into 'Apache', so that whatever
the methods are there in this 'Tomcat', all methods are valid
in this 'apa_ob' also. But initialization method is
already there here and here, that's why preference is for this
initialization method, not for this.
[no audio]
Right. So this is the way how we can inherit a method of one class
into other class. What we are doing? Simply while creating
your second class
we are writing in place of parentheses, in place of object
we are writing your required class from which you need to
inherit, from which you need to import the methods, that class
name you have to write so that the methods which are available
in your this class will be available with this object
also, I mean, whatever the object you are going to create
from this class also. That's it.
That is the simple way to understand inheritance concept.
Anyway, you will get more ideas while writing our real-time scripts.
Okay. So finally, what is the advantage by using inheritance? Simple.
This saves you from duplicating a lot of code. Suppose
you are defining same method in different classes and with the
same code. Why should we repeat same code in different classes?
Define in one class and inherit that class into your current
class, so that you can use the previous class methods in the
current class also. That is the advantage. And one more thing.
Yeah, last point.
[no audio]
For 'Apache', we are inheriting methods from 'Tomcat'. Now, here I
can say 'Tomcat' is the parent class and 'Apache' is the child class,
or 'Tomcat' is the superclass and 'Apache' is the subclass.
You need to remember those names.
Parent, 'Tomcat' or superclass is 'Tomcat'. 'Apache' is the child
class or subclass.
Right. That's it guys.
Thank you for watching this simple video.
[no audio]