Hello everyone.
So in this video I want to discuss about inheritance.
OK.
So what is the meaning of inheritance.
So inheritance means to inherit some properties from our parents or ancestors.
OK.
So just like we humans do what we do we all inherit some correct districts from our parents or grandparents
and some properties or qualities are our own.
OK so let's say in code.
So suppose I have three classes
bicycle and I have a class truck.
Okay.
So the properties of car class are color color of the car maximum speed of the car and let's say the
number of gears.
Okay.
So number of gears and louder let's talk about the properties of bicycle to color of the bicycle max
speed and obviously bicycle doesn't have gears.
So let's say we have a billion property is for level.
So some bicycle s for level.
So we have a property billion.
Property is for level.
Okay.
Now let us talk about truck.
So conserve the truck max speed of their truck and some other drug related properties okay.
So what is happening here is some properties are common in many classes.
Okay.
So common common and common.
Okay.
So these properties and some properties are common in many classes because they all have one feature
in common all three belongs to vehicle.
Okay.
All three are vehicle.
So what is happening here is so I have a class vehicle.
So all the three things car bicycle and truck they all have one feature in common.
All three are vehicles.
Okay.
So now my aim is I have to create these three classes okay.
So I have to create three classes
guide bicycle and truck.
So the first race just like we created these classes.
So the first race we will create classes in this manner.
First some common properties then candidate properties.
Then again some common properties then bicycle related properties.
Again some common properties and then regulated properties.
Okay.
Now the second way to create these three classes in a good way in a in an appropriate way.
It is what we will do.
We will move these properties inside difficult class okay so that we could class will have color and
the vehicle will obviously have the max speed.
Okay.
And we will remove it from here.
Okay.
So remove these things from the bicycle class.
Remove the common properties from the track class okay.
So we have to move.
Common properties to difficult class.
So vehicle is our parent vehicle is our parent class and car is a child.
Child class bicycle is a child class truck is a child a class okay.
So what really happened that car class will have some kind of specific properties and the common properties
it will inherit from its parent.
Okay.
Similarly bicycle will have some properties of its own bicycle later properties and other properties
it will inherit from vehicle.
Similarly truck will have some percolated properties truck specific properties and the common properties
it will inherit from its parent that is difficult class.
Okay.
Now suppose I want to create one more class here.
Okay so I want to create one more class.
Let's say the name of the class is bike so the name of the classes bike.
Now what bike will do.
It will inherit some common properties it will inherit color and the max speed from the vehicle class
and some properties will be related to the bike specific properties.
Okay so the second way is the most appropriate way to create classes.
Okay so whatever approach.
Move.
Common properties to a common class to one class create one class which is the parent of all the classes
and move the common properties there.
Okay.
So let us take one more example to understand it better.
OK so suppose I ever teach a class and I have a student class teacher class end student class.
So now let us talk about some properties of the teacher.
So teacher will have a name age of the teacher will be a property.
Similarly address contact number
and let's say qualifications without the qualification of their teacher expertise area.
So these are some properties of a teacher.
Now let us talk about the properties of the student.
So a student will have a name and will have age similarly address contact number
a student will have.
Rule number badge number and so on.
Okay so now we can see here again some properties are common name age address contact number and here
also some properties are common.
Okay so now what we will do we have learned about inheritance.
We will create a new class.
We will create one class let's say the name of this class is human class.
We will move the common properties here and we will remove the common properties from here okay.
So remove the common properties because the common properties will now belong to human class.
So all human will have name it will have each.
It will have address and it will have contact number
so.
Teacher class will contain only teacher specific properties.
Student class will contain only student specific properties and all the other properties.
What it will do.
It will inherit from its parent.
Okay so we will inherit the common properties from our parents.
Again we will inherit some properties from our parents and some properties will be our own.
Okay so we are inheriting common properties Okay so human is our patent class human is our parent and
teacher and student.
They both had a child.
Child classes okay.
So what is the main purpose.
What is the main benefit of using inheritance.
So the main benefit of using inheritance is reusing the called
We are reusing the code.
Okay so move all the common things at one place and whenever that common thing is acquired just inherited
the common properties.
Okay so this is the main benefit of using inheritance reusing the gold.
Okay.
We will just write the code once and if someone wants those properties just inherit the properties.
Okay.
So the main benefit of inheritances reusing the code
okay.
Now let's talk about excess modifiers.
Now let's talk about excess modifiers
so we already know about to excess modifiers public and private.
So we have already started about private and we have already started about public so private means it
is not accessible outside the class and public means it is accessible to anyone outside the class.
Okay so they both are very extreme.
Private is very strong and public is very lenient okay.
So we want something in between and there is something in between the public and private is called protected.
Okay.
So what is protected.
So what will happen.
Let's say I have a class and inside the class if I declare a property X to be a protected property.
So what will happen.
So this protected property is accessible to someone outside the class and is not accessible to the others.
Okay.
So if I declare a property X a protected property for this protected property will be accessible outside
the class for only for some people and for that it will not be accessible okay.
So X is a property I've owned the property that would be accessible for some person and not accessible
for the rest of the world.
So we will declare that property as a protected property.
Okay so let's say I have two classes Class A and Class B Class A is a parent class and class B is a
child's class.
Okay.
So B inherit some property from me.
Okay so let us take the call in God.
So I have vehicle and God.
So God is a child.
Class and vehicle is a parent.
Class Okay so God will inherit some of its properties from the vehicle class.
Okay so what will happen if a property is protected then it is only accessible to child class.
So let us take an example.
So suppose this is working class and inside the working class.
I have declared a property X as a protected property.
So what will happen.
So this protected property X is accessible outside the class but only for those classes which inherit
from the vehicle class.
Okay so this protected property X is accessible outside the class but only for those classes which inherit
from vehicle class.
That is only the child classes of the class.
Okay so what I am saying is.
So what I am saying here is let's say I have a vehicle class so I have a class vehicle inside the vehicle.
I have a public property X Y and Z so access public values private and Z is protected
and this is outside.
Okay so public property X anyone can access public property X outside the class.
Okay.
X is accessible for everyone okay.
Anyone can access anyone can access X because X is a public property outside the class private property
y no one can access the private property why outside the class protected properties and it is accessible
outside the class but using only roads child classes using only its child classes
okay.
So public property X anyone can access outside the class no one can access outside the class it is accessible
outside the class but using only it.
Child classes Okay so suppose this is our vehicle class and there are three other classes which I inherited
from the working class and these are car is a child class of vehicle.
Bicycle is also the child class of vehicle and truck is also the child the class of vehicle.
Similarly let's say I have a class human so a human teacher and student inherits from human.
Okay now let's talk about protected properties and okay so obviously vehicle can access the protected
properties and God can access the protected property.
Bicycle can access that and truck can access it and why because the oil out of child classes or difficult
class so human cannot access that teacher cannot access it and student cannot access it.
Okay so this is human cannot access student cannot access and teacher cannot access the protected property.
Z of the class.
Okay so if you want to access the protected property there should be a relation between the parent class
and the child class.
Okay.
So I hope you know what is protected now so protected properties only accessible using.
Child classes okay since human does not have any relation with the vehicle so it cannot access Z if
human cannot exist then obviously teacher and student those who cannot access okay.
Thank you.