Hey, in this session, we will discuss about inheritance, so inheritance allows us to create a new
child clause which is based on the existing betting class.
In this particular case, the child class will have all the attributes and methods of the best in class,
which can be modified internally in the first class as well.
So what does this mean?
So here inheritance is a way to form new classes using classes that has already been defined.
So let's say we have created a class person so a person will have certain attributes and methods.
So let's see the attributes related to the person I name, age, address.
Now, the methodology to the person would be, let's say dog, walk, eat.
Now, these are Gendron methods and general attributes related to a person.
Now, whenever I'm creating an employee or a doctor or an engineer in that particular case, will I
be writing the method and the attributes which are pre existing again?
So ideally, what I should be doing is if I don't really know about inheritance, I would be creating
an employee class and my employee class will be having certain attributes, for example, name, age,
address of the person, and additionally something which is specific to employees such as their employee
and their salary.
And the methods would be, let's say, work or travel to office, come back from office.
These are something related to the employee.
Now, whenever I am creating a doctor, what will happen now?
The doctor will have something like diagnose, then perform a surgery.
And apart from that, it will again have the ones which will probably present.
Right.
So, for example, name, address and eat, sleep, work, because doctors will also perform all the
actions and religious rituals are performed by a person.
So here what we get to see is that whatever behavior was present in a person actually comes inside or
engineer or a doctor or a student or a human being, anything like any such activity.
Right now what we are doing is when we are creating this specific class, such as doctor here, we are
adding a few methods additionally.
Right.
So basically it has different methods and attributes from the parent class, the art of the present,
which would be present in to the better.
So I don't create these methods.
Again, I just Dellec that.
Okay, you are, doctor.
So this class of doctor will contain all the methods which are specific to Bochner and also inherit
all the methods and attributes which are already present and created when we created a person so that
we don't have to create things again and again and again.
Right.
Similarly, even I have created a doctor.
Now this doctor is actually, let's say, a neurosurgeon.
So the neurosurgeon will have some more specific details and some more specific tasks which he will
perform.
So now this class will inherit from the person class.
Also, I'm from the doctor glassless.
Right.
So this can create a change.
So we can what we can do is whenever I'm creating a particular class, if I need something which needs
to be repeated, if I need certain behaviors and attributes which are already present in some other
class, I can simply inherit from that class.
I mean, I inherited from that class.
The class from which I am inheriting is for the best class or the better in class and the class which
I am inheriting to the new class of the day and creating is called Giedd Class or derived class.
OK, so this is the basic concept behind inheritance.
Now let us go back and try to create this thing and implement this thing.
So let us go ahead with inheritance.
Now, here's what we need is we are creating a new middle class.
OK, so my class is animal.
Now, what does an animal do?
This is my divine creator in it.
OK, this is the initialization it takes self and it to
create an animal created.
Now, let me create another method and say this.
This is animal created.
Another one is say.
This one is
who am I?
OK.
And this says that this is animal.
Next I am creating.
E!
And what does it do?
It says it OK, now I'm creating another class now.
This class is what this class would be, a dog class.
OK, now I'm creating a dog class.
Now let's create the dog class.
So I said, OK, now I want this dog class to inherit these initialization.
Who am I this eat from the animal class.
OK, I want this to get all the stuff which is present in my animal class.
It because dog is what dog is up animal.
So I want those things to be already presented.
I don't want to write it down again.
So what I simply do is I will tell it like you need to inherit stuff from animal class.
OK, now this understand when I write it inside the brackets, it's understanding that I need to inherit
from this animal class.
Now, in the initialization, I would say that dog has been created by, say, dog created and I'm asking
where am I?
So I'll tell it that, OK, this is a dog, OK?
And I create another function which is bark, OK?
And when it says barking, it's where it will say, well, OK, so this is what I am doing now.
What have we done now?
We have created an animal class.
We have created a dog class in animal class we have initialization.
So when an instance of animal would be created, it will say animal created, then we will create the
object.
I'm from on top of that object.
We will then where might will say animal.
Then we will ask it to eat.
It will say eating.
When we create an object of dog it will say dog created when we run this.
Who am I from the dog object it will say dog.
And when we say bark it will say OK so let us run this one.
So it has been run.
Now what will create will create an object of the dog.
So we will see dog one is equal to dog.
So we are creating an object of the dog.
Plus what happens now when I run this.
It says that dog created ok.
See, this is dog and the same dog created.
Now let's make a little slight change.
OK, now what do you see?
I asked that whenever you are initializing the dog, initialize the animals.
So what we can see from the animal class.
OK, then, because we are calling the animal Glaswegians, the animal that we were talking about,
any object, then we would have referred to the object itself here.
OK, so we are seeing animal dot in it and inside this we are putting.
OK, now see the difference.
OK, so when we done this just a moment I'll have to run this again.
When we done this we animal created.
Both created.
Now see the difference.
OK then we wrote only this in it and only this particular in it.
We have not written this particular statement.
It did not run the initialization of the animal flush.
OK, if you want to run the initialization of the animal class also, then you will have to run the
animal in here, OK?
Otherwise it will run only initialization for the book.
OK, now let's go for the next thing we say.
Dog one dot.
I OK, and she wondered, why am I so what does it say?
It says Dog, OK, now why is it saying Who am I?
Because it has inherited it would have inherited this particular who am I now?
Because this is present here now we have overcome this.
OK, now this would have came down here.
Now let me do one thing.
Let me just comment this week.
Now, when I go this and I run this again, I run this again and I create this object again and I run.
Bob wondered, who am I now I get and what do I why do I get animal?
Because this is inherited.
So this comes down here.
So it as it is assuming that when I am running, Bob wondered why am I.
Because there is nothing as women here.
So it is getting the metal from the base class.
But when we come this, when we actually define who am I inside the child plus then it will give preference
of priority to the Who am I present in detritus.
So whatever was written in this animal class actually get over it.
And when we are running, who am I from the book.
One that is the object of the dog class.
It will run whatever is present here.
So if I run this then it will say dog.
OK, next, if I say let's say dog one dog
eat dog I say eat.
So what happens.
It says eating because it has inherited this method from the parent class because it does not present.
So it will run whatever it was.
But as an independent class it now again same thing applies to the bark.
OK, so when I say dog one don't bark, then it will say because this is a new method which we have
created in the dog and that is a Gigas.
So now you understand what is happening.
So basically what we are doing is if we have any method which was in the best class and is not present
in the jungle, then it will run whatever it is, and basically it will run that method of method from
the base class.
If you have over it in the method in the gym class or you have newly created the method method in the
gikas, then it will run it from each class.
It's as simple as that.
Nothing complicated it.
OK, so next thing, what we will be learning about is polymorphism.
So let's talk about this in the next session.