In this session we will talk about the object oriented programming principle inheritance inheritance
as the name suggests is a process of inheriting features.
Inheritance is a technique of building new classes from an existing class by inheriting its features
and the features include different attributes and methods let's understand inheritance.
Using an example so here on the screen I have defined two classes on the left hand side of the screen.
We have a class basic geek which defined and a net method which lets down the ingredient and there is
another method procedure which describes the processes of the procedure to make a basic geek and to
the right hand side.
I have defined another class chocolate cake which has two methods which defined the stuffing deviant
and then the procedure to make a chocolate cake.
If you observe carefully most of the code in these classes has been repeated or duplicated and in the
programming world duplicate code is not recommended we should not have multiple copies of the same or
similar code in different places.
And there are many ways to MOJ similar pieces of code or objects with similar functionality.
In this example I have defined a class cake which again has two methods.
The init method and then the procedure method and then I have also defined another class chocolate cake.
But this time I have passed the class that I have defined about cake as a parameter to this new class.
Chocolate cake and this is how we tell Python that the new class should be inherited or derived from
the existing class.
The new class is called child class or a subclass the existing class is called the parent class
or superclass
or the base class coming to our child class or subclass chocolate cake.
I have defined and init method here and these are the parameters that I have passed to the child classes
in that method and I have initialized the instance attribute self-taught cook or all the parameters
flawed sugared Butter and eggs have already been initialized in the parent classes init method
and now we have to make a call to the parent classes in a method in order to initialize all of these
attributes.
So how do we do that.
And for this I'm going to use the super function the super function enables us to access the method
and the attributes of a parent class without referring to the parent class by name using super.
The instance of the parent object is retrieved post and then the init method is called on that object
passing in the arguments here.
The floor should good butter and eggs in another example the super function is a built in function which
returns a proxy object that allows us to therefore paid in class by super so instead of calling the
inert method using the last name we call the init method using the built in function super it so that
we can easily change the in class name when required.
So by inheriting the attributes from the parent class a lot of duplicate code has been awarded in the
child class and inheritance enables us to define a child class that takes all the functionality from
a parent class and also allows us to add more.
So let's see some examples in the Jupiter notebook.
Let's discuss another example in the Jupiter notebook your I have defined a class school and in this
class there is an inert method.
So these are the two parameters that we are opposed to the net method the school name and then the location
and then we have initialized the instance attributes not let execute this in and then let us create
an instance of this class school.
So let's say the instance is a b c and then we will pass the the argument ABC school the location so
ABC is an object of the class school.
Now I am also going to create another class called teacher so let's do that.
I have defined another class teacher and this time the existing class school has been passed as a parameter
to the teacher class.
So the last school is the parent class and the class teacher is the child class and in the new class
or the child class I have defined and init method.
The init method has these parameters the teacher name subject school name and location and then we have
initialized all of these attributes and we know that we should not have multiple copies of the same
or similar code in different places.
So we need to have a way in order to make a call to the parent classes init method in order to initialize
these attributes.
And for that we can make use of the super function.
So we are going to commend the duplicate code in the child classes in that method and then make a call
to the parent classes in that method.
For that let's use the super function S P E.
So but DA and super returned the object as an instance of the parent class allowing us to call the parent
method directly.
So let's call the init method on the parent classes instant underscored underscored in it underscored
underscore and we want to initialize the attribute school name and location.
So using inheritance a lot of duplicate code has been removed.
Now let's add another method to the to the parent class.
Now to the parent class I have added a new method details which print the school name and the location.
So here is the instance of the school class.
So let's call the detailed method.
ABC dot details.
So here we have the details of the school.
Now let us create an instance of the class teacher that is the child class so let's call this instance
as Melinda and then the argument is to this glass that is a child loves the teacher name and the subject
school name.
A B C school and then location let's execute this.
The class teacher is a child class and it has inherited the attributes and the methods of debate in
class school.
We can call the methods of the iPad and class which have not been defined in the child class so let
us call.
So let's call the details method using an instance of the child class.
Melinda dot details let's call the method details using the child class instance executed.
So you have the school name and the location displayed.
Now I want to add a few more details.
I want to add more code to the method details which also displays the name of the teacher and the subject
that she teaches in the child class.
I have added another method details and this method defined in the child class has the same name as
the method independent class school.
So what is going to happen if you call the detailed method on on a child classes instance.
No let us call the details method.
This time we have called the details method using the child classes instance and the detailed method
of the child's class has been accessed.
And this is known as overriding overriding is the ability of a class to change the implementation of
a method provided by one of its parent classes so instead of implementing the method provided by the
parent class that is the details method independent class using method or what writing.
We have implemented the method that is defined in the child class so using inheritance we can implement
the the method defined in the parent class.
But if we all would write the method in the child class the methods provided by the better class will
not be considered at all next.
Say for example apart from displaying the teacher name and the subject you also want to display the
school name and the location in the output menu implement the detailed method in the child class.
That is the teacher class but we already have a method in the parent class school which through which
we are displaying the school name and the location.
We can make a call to the to the details method of the parent class using the super function so using
the super function we can call the good and method of the parent class from inside the child.
Class so as of now we cannot implement the details method of the parent class.
That's because the details method of the child class is being implemented and both these methods have
the same name.
If you want to make a call to the overwritten method of the parent class you can use the super function.
So not just in the insert method the super function can also be used in other methods.
So super dart this time we are going to make a call to the details method of the in class and you're
not going to pass any parameters.
It's a good.
This now when you call the details matter in the output we see all of the attributes displayed the first
two attributes are being displayed using the details method of the child class and the last two attribute
school name and location are being displayed using the details method of the parent class.