1 00:00:00,230 --> 00:00:01,040 Hello, guys. 2 00:00:01,040 --> 00:00:02,330 Good to see you back once again. 3 00:00:02,330 --> 00:00:06,560 I'm back with another tutorial related to object oriented programming in JavaScript. 4 00:00:06,950 --> 00:00:10,220 And in this lesson we are going to learn about inheritance. 5 00:00:10,250 --> 00:00:13,190 Now the question is what is inheritance? 6 00:00:13,830 --> 00:00:19,650 In our previous tutorial, we learn a lot about classes, how we can create constructors, how we can 7 00:00:19,650 --> 00:00:22,170 create property in class with that. 8 00:00:22,170 --> 00:00:25,110 Also, we learn how we can create object using class. 9 00:00:25,740 --> 00:00:28,770 And now we are going to learn about inheritance. 10 00:00:28,800 --> 00:00:37,290 Suppose I create a class and our class name is a and this class have its own property and methods. 11 00:00:37,650 --> 00:00:42,180 At the same way we have another class and our another class name is class B. 12 00:00:42,570 --> 00:00:46,110 It is also have its own property and methods. 13 00:00:46,560 --> 00:00:51,090 Now the question is can we use class property and method from class B? 14 00:00:51,450 --> 00:00:52,560 No we can. 15 00:00:53,100 --> 00:00:56,650 We cannot use class A properties and methods in a simple way. 16 00:00:56,670 --> 00:00:59,490 For that we need to use inheritance. 17 00:00:59,910 --> 00:01:02,460 Inheritance is going to allow us to do it. 18 00:01:02,490 --> 00:01:09,510 Suppose if I want to access class properties and method from class B, in that case class A become base 19 00:01:09,510 --> 00:01:15,840 class and the class who are going to use base class property is called the right class. 20 00:01:16,080 --> 00:01:20,820 Not only that, we can create another derived class which is class C. 21 00:01:21,090 --> 00:01:25,220 Now class C can able to use class B properties and method. 22 00:01:25,230 --> 00:01:27,330 Also class A properties and method. 23 00:01:27,600 --> 00:01:35,670 This is the single level inheritance and this is called multi level inheritance because class C derived 24 00:01:35,670 --> 00:01:38,400 with class B also class A. 25 00:01:38,820 --> 00:01:41,760 I know it's look pretty confusing for you. 26 00:01:41,790 --> 00:01:45,300 Let me give you the real life and graphical example. 27 00:01:45,390 --> 00:01:46,380 Inheritance. 28 00:01:46,380 --> 00:01:52,410 Allow us to define a class that inherits all the methods and properties from another class. 29 00:01:52,830 --> 00:01:56,880 Now let's try to understand it in a simpler terms with a picture. 30 00:01:57,120 --> 00:02:03,490 Suppose this is your grandfather, and your grandfather owned a lot of land in the past. 31 00:02:03,510 --> 00:02:08,760 So at object oriented term, we can call your grandfather grandparent class. 32 00:02:08,880 --> 00:02:12,310 And then he decided to give all the lands to his son. 33 00:02:12,330 --> 00:02:13,620 Means your father. 34 00:02:13,950 --> 00:02:17,070 I want to say your father get property from your grandfather. 35 00:02:17,610 --> 00:02:20,970 But your father himself owned a lot of properties. 36 00:02:21,330 --> 00:02:23,370 Your father bought many shares. 37 00:02:23,670 --> 00:02:27,600 So your father now owns two type of properties. 38 00:02:27,960 --> 00:02:32,400 So he can sell his land and his own shares if he wish. 39 00:02:32,820 --> 00:02:38,520 But your father decide to transfer his ancestral property and shares to your name. 40 00:02:38,760 --> 00:02:42,200 But you yourself have made a lot of properties. 41 00:02:42,210 --> 00:02:44,700 You have lot of cryptocurrencies. 42 00:02:45,270 --> 00:02:48,480 So if you want, you can use your own cryptocurrency. 43 00:02:48,510 --> 00:02:50,310 Otherwise your father shares. 44 00:02:50,340 --> 00:02:56,670 Otherwise your grandfather lands, but your grandfather cannot use your father. 45 00:02:56,670 --> 00:02:57,240 Share. 46 00:02:57,270 --> 00:03:03,900 Also, your father cannot use your cryptocurrencies, but your father can use his father lands. 47 00:03:03,900 --> 00:03:08,160 But your grandfather is not authorized to use his son's shares. 48 00:03:08,490 --> 00:03:14,760 Similarly, your father cannot use your cryptocurrencies, but you have to access all of it. 49 00:03:14,790 --> 00:03:19,440 You can use land, you can use shares, and also you can use your own cryptocurrencies. 50 00:03:19,710 --> 00:03:23,630 Basically we control repeatability using inheritance. 51 00:03:23,700 --> 00:03:27,270 Now let's understand it with object oriented term. 52 00:03:27,750 --> 00:03:32,820 Suppose your grandfather land is a function and your father share is also function. 53 00:03:32,910 --> 00:03:36,830 So you don't need to type both the function in your child class. 54 00:03:36,840 --> 00:03:43,710 You can access it when you want, but your grandfather and your father cannot access your properties. 55 00:03:44,190 --> 00:03:47,070 I hope now it's clear for you what is inheritance? 56 00:03:47,190 --> 00:03:50,340 I have tried to explain you in a very simple way. 57 00:03:50,370 --> 00:03:52,800 Now let me show you this syntax. 58 00:03:52,800 --> 00:03:54,120 How it works. 59 00:03:54,420 --> 00:03:57,750 Suppose I have a class which is fruits. 60 00:03:57,840 --> 00:04:03,630 And now I want to create another class which is able to use class fruits properties and method. 61 00:04:04,110 --> 00:04:09,330 And our class name is vegetables which is extended with fruit class. 62 00:04:09,690 --> 00:04:17,910 So in our case base class is fruits and vegetable is the right class which is derived with fruits. 63 00:04:18,120 --> 00:04:23,670 And here we need to use a special keyword which is used to derive it with another class. 64 00:04:23,670 --> 00:04:26,520 And the keyword is extends. 65 00:04:26,700 --> 00:04:28,950 Next we need to pass the base class name. 66 00:04:29,250 --> 00:04:32,930 And in our fruit class we have some properties and methods. 67 00:04:32,940 --> 00:04:36,210 Then create a object using fruit class. 68 00:04:36,240 --> 00:04:39,330 Let f equal to new fruits. 69 00:04:39,330 --> 00:04:46,110 So f is our object and this object is able to use only fruit class property and methods. 70 00:04:46,500 --> 00:04:53,910 But if I create an object using vegetable class, suppose let v equal to new vegetable. 71 00:04:54,000 --> 00:04:58,370 Then this object can access its own properties and methods. 72 00:04:58,380 --> 00:05:01,830 Also it can access fruit class properties and methods. 73 00:05:02,010 --> 00:05:04,140 So this is how inheritance work. 74 00:05:04,290 --> 00:05:07,380 So let's start the practical and see how we can use it.