1 00:00:00,690 --> 00:00:01,350 Welcome back. 2 00:00:01,620 --> 00:00:07,830 And this video we're going to cover abstract classes and abstract classes are a little bit similar to 3 00:00:07,830 --> 00:00:11,190 interfaces, but they are still different, so let's look at them. 4 00:00:11,700 --> 00:00:14,070 An abstract class cannot be instantiated. 5 00:00:14,400 --> 00:00:15,720 That's the same for interfaces. 6 00:00:15,720 --> 00:00:17,880 You can create an object of an interface. 7 00:00:18,330 --> 00:00:24,330 However, you can inherit subclasses from an abstract class and the members. 8 00:00:24,420 --> 00:00:29,850 So properties and the methods of an abstract class are not abstract unless you explicitly use the abstract 9 00:00:29,850 --> 00:00:31,320 key word to make them abstract. 10 00:00:31,650 --> 00:00:33,000 So let's have a look at an example. 11 00:00:33,540 --> 00:00:40,410 So I have this abstract class memo here, which has a constructor here, a primary constructor with 12 00:00:40,410 --> 00:00:42,660 a name and origin and the weight. 13 00:00:43,380 --> 00:00:47,250 And these are concrete, non abstract properties. 14 00:00:47,610 --> 00:00:47,880 All right. 15 00:00:47,880 --> 00:00:50,660 So the three that we created here name origin and weight. 16 00:00:50,670 --> 00:00:51,930 They are non abstract. 17 00:00:52,650 --> 00:01:01,440 But this one here is so I create a property called Nextbit, which is abstract, so it must be overridden 18 00:01:01,740 --> 00:01:03,180 by its subclass. 19 00:01:03,420 --> 00:01:10,170 So if the class inherits from Memmo, then it will need to implement those. 20 00:01:10,380 --> 00:01:11,820 So it needs to override those. 21 00:01:12,390 --> 00:01:13,680 The same goes for these methods. 22 00:01:13,740 --> 00:01:18,120 You can see we can create methods without a body. 23 00:01:18,420 --> 00:01:24,270 So we just add the abstract key word abstract fun and make run an abstract function. 24 00:01:24,840 --> 00:01:34,350 The same goes for a brief here, and then we can also create concrete so abstract methods in such an 25 00:01:34,350 --> 00:01:35,130 abstract class. 26 00:01:35,430 --> 00:01:37,840 So we're not limited to abstract methods. 27 00:01:37,860 --> 00:01:43,500 We can also just create a method such as display details that we have here, which will just display 28 00:01:43,500 --> 00:01:48,330 the name origin, weight and speed of the mantle that we're creating. 29 00:01:48,690 --> 00:01:51,750 So now let's look at this class that inherits from mammal. 30 00:01:52,350 --> 00:01:57,240 It has also the relation to weight and the max speed. 31 00:02:00,380 --> 00:02:06,290 It has also the name the region, the weight, but it also overrides the max speed. 32 00:02:06,500 --> 00:02:09,680 So this abstract property that we created here. 33 00:02:10,130 --> 00:02:13,460 So now our human class will overwrite that. 34 00:02:14,700 --> 00:02:20,760 Then we also override the run function, so we implement the run function with our own code for all 35 00:02:20,760 --> 00:02:21,240 human. 36 00:02:21,810 --> 00:02:23,280 We do the same for breath. 37 00:02:24,390 --> 00:02:29,370 And then we have another class called Elephant, which does the same thing, so it overrides the next 38 00:02:29,370 --> 00:02:35,700 beat and is implements its own code for run and it's uncalled for breath. 39 00:02:36,390 --> 00:02:41,460 Of course, they could both have a bunch of more functions and a bunch more properties. 40 00:02:41,670 --> 00:02:43,980 But I'm going to keep it super simple in this example. 41 00:02:44,790 --> 00:02:45,180 All right. 42 00:02:45,450 --> 00:02:52,290 And now we can go to our function May, where we create an object of human, which is called Dennis, 43 00:02:52,290 --> 00:02:59,280 was born in Russia as a weight of 70 kilos and a speed of 28 kilometers per hour, even though I'm not 44 00:02:59,280 --> 00:03:00,600 sure whether that is true. 45 00:03:00,960 --> 00:03:04,320 So then we have the elephant, which is called Rosie. 46 00:03:04,320 --> 00:03:11,220 It's from India that weighs 5400 kilos, and it has a speed of 25 kilometers per hour. 47 00:03:11,550 --> 00:03:16,650 So as it seems, I could outrun Rosie, but depends on how long I can do that. 48 00:03:17,010 --> 00:03:23,950 So then if we call Human Run, it's going to execute the run functionality for the human, says runs 49 00:03:23,970 --> 00:03:24,900 on two legs. 50 00:03:25,320 --> 00:03:30,360 And if we called the same for the elephant, it will say runs on four legs. 51 00:03:30,660 --> 00:03:31,920 So let's just test it. 52 00:03:32,520 --> 00:03:39,690 By the way, we can also see that we are breathing so the human caused breathing and the elephant calls 53 00:03:39,690 --> 00:03:40,020 breathe. 54 00:03:40,860 --> 00:03:41,700 So there we are. 55 00:03:41,880 --> 00:03:44,960 We see it says runs on two legs, runs on four legs. 56 00:03:44,970 --> 00:03:47,460 Breathe through mouth or nose. 57 00:03:47,460 --> 00:03:49,200 Breathe through the trunk. 58 00:03:49,830 --> 00:03:54,810 So I'm not saying breaths here because it just needs to breathe. 59 00:03:54,970 --> 00:03:56,610 So I force it to breathe. 60 00:03:56,730 --> 00:03:57,090 All right. 61 00:03:57,600 --> 00:03:58,110 Chromatically. 62 00:03:58,110 --> 00:03:58,920 Still correct? 63 00:03:59,520 --> 00:04:00,010 All right. 64 00:04:00,030 --> 00:04:03,660 And that is already the major part about abstract. 65 00:04:03,990 --> 00:04:08,910 And let's have a try to create a mammal here. 66 00:04:11,840 --> 00:04:20,860 Memo, and I'm going to say, Dennis, it's a memo, so here then, is Russia. 67 00:04:21,970 --> 00:04:31,750 Then weight of seventy point seven and finally a speed of 28.0, so trying to do that, you can see, 68 00:04:32,050 --> 00:04:36,700 cannot create an instance of an abstract class, so you cannot do that. 69 00:04:37,180 --> 00:04:41,920 You can, however, of course do that with classes that inherited from it. 70 00:04:42,700 --> 00:04:51,220 So our human and our elephant, which are both classes that inherit from our abstract class called mammal. 71 00:04:51,880 --> 00:04:52,270 All right. 72 00:04:52,270 --> 00:04:57,220 So what is the difference between an abstract class and an interface? 73 00:04:57,310 --> 00:05:02,200 Because you might say, all right, there must be pretty much the same right or they're very similar 74 00:05:02,200 --> 00:05:02,800 to each other. 75 00:05:03,250 --> 00:05:11,560 Well, the thing is that an interface cannot hold state and we can implement multiple interfaces, but 76 00:05:11,560 --> 00:05:13,120 only one class. 77 00:05:13,720 --> 00:05:20,920 So when implementing an interface, as we have done in the last example here where we implemented driver 78 00:05:20,920 --> 00:05:24,430 pool, we could implement multiple interfaces here. 79 00:05:24,610 --> 00:05:29,580 We're not limited to just one interface, but we can only inherit from one class. 80 00:05:31,260 --> 00:05:35,910 Also, classes have constructors, so even an abstract class has a constructor. 81 00:05:35,970 --> 00:05:39,960 You can see that our interface doesn't have a constructor here. 82 00:05:41,470 --> 00:05:48,370 And the faces cannot hold fields, so basically abstract classes can have everything that interfaces 83 00:05:48,370 --> 00:05:49,010 can add. 84 00:05:49,030 --> 00:05:51,880 Additionally, they can have fields and constructors. 85 00:05:52,420 --> 00:05:56,350 Therefore, we can properly hold state and abstract class. 86 00:05:57,250 --> 00:05:57,670 All right. 87 00:05:57,670 --> 00:06:00,370 So in the next video, we're going to check out typecasting. 88 00:06:00,370 --> 00:06:01,330 So see you there.