Hello, guys.
Good to see you back once again.
I'm back with another tutorial related to object oriented programming in JavaScript.
And in this lesson we are going to learn about inheritance.
Now the question is what is inheritance?
In our previous tutorial, we learn a lot about classes, how we can create constructors, how we can
create property in class with that.
Also, we learn how we can create object using class.
And now we are going to learn about inheritance.
Suppose I create a class and our class name is a and this class have its own property and methods.
At the same way we have another class and our another class name is class B.
It is also have its own property and methods.
Now the question is can we use class property and method from class B?
No we can.
We cannot use class A properties and methods in a simple way.
For that we need to use inheritance.
Inheritance is going to allow us to do it.
Suppose if I want to access class properties and method from class B, in that case class A become base
class and the class who are going to use base class property is called the right class.
Not only that, we can create another derived class which is class C.
Now class C can able to use class B properties and method.
Also class A properties and method.
This is the single level inheritance and this is called multi level inheritance because class C derived
with class B also class A.
I know it's look pretty confusing for you.
Let me give you the real life and graphical example.
Inheritance.
Allow us to define a class that inherits all the methods and properties from another class.
Now let's try to understand it in a simpler terms with a picture.
Suppose this is your grandfather, and your grandfather owned a lot of land in the past.
So at object oriented term, we can call your grandfather grandparent class.
And then he decided to give all the lands to his son.
Means your father.
I want to say your father get property from your grandfather.
But your father himself owned a lot of properties.
Your father bought many shares.
So your father now owns two type of properties.
So he can sell his land and his own shares if he wish.
But your father decide to transfer his ancestral property and shares to your name.
But you yourself have made a lot of properties.
You have lot of cryptocurrencies.
So if you want, you can use your own cryptocurrency.
Otherwise your father shares.
Otherwise your grandfather lands, but your grandfather cannot use your father.
Share.
Also, your father cannot use your cryptocurrencies, but your father can use his father lands.
But your grandfather is not authorized to use his son's shares.
Similarly, your father cannot use your cryptocurrencies, but you have to access all of it.
You can use land, you can use shares, and also you can use your own cryptocurrencies.
Basically we control repeatability using inheritance.
Now let's understand it with object oriented term.
Suppose your grandfather land is a function and your father share is also function.
So you don't need to type both the function in your child class.
You can access it when you want, but your grandfather and your father cannot access your properties.
I hope now it's clear for you what is inheritance?
I have tried to explain you in a very simple way.
Now let me show you this syntax.
How it works.
Suppose I have a class which is fruits.
And now I want to create another class which is able to use class fruits properties and method.
And our class name is vegetables which is extended with fruit class.
So in our case base class is fruits and vegetable is the right class which is derived with fruits.
And here we need to use a special keyword which is used to derive it with another class.
And the keyword is extends.
Next we need to pass the base class name.
And in our fruit class we have some properties and methods.
Then create a object using fruit class.
Let f equal to new fruits.
So f is our object and this object is able to use only fruit class property and methods.
But if I create an object using vegetable class, suppose let v equal to new vegetable.
Then this object can access its own properties and methods.
Also it can access fruit class properties and methods.
So this is how inheritance work.
So let's start the practical and see how we can use it.