1 00:00:00,450 --> 00:00:01,140 ‫Welcome back. 2 00:00:01,170 --> 00:00:04,020 ‫In the last video, you saw the basics of abstract. 3 00:00:04,200 --> 00:00:06,780 ‫And I want to show you a little more about that. 4 00:00:06,780 --> 00:00:09,690 ‫So let's go ahead and and create a cube. 5 00:00:09,870 --> 00:00:12,330 ‫And it's going to it's going to be Ice Cube Baby. 6 00:00:12,600 --> 00:00:16,050 ‫And it's going to have a shape as a cube. 7 00:00:17,360 --> 00:00:17,660 ‫All right. 8 00:00:17,660 --> 00:00:24,860 ‫So this pretty much means that I want to create a cube, and it has to be the shape that we're currently 9 00:00:24,860 --> 00:00:26,950 ‫looking at as a cube. 10 00:00:26,960 --> 00:00:33,920 ‫So we're using this as a keyword in order to say I want to have it as a specific class type, whereas 11 00:00:33,920 --> 00:00:39,340 ‫a specific data type, the data type is cube, and that's the data type that we created ourselves. 12 00:00:39,350 --> 00:00:41,570 ‫It's like saying something as INT. 13 00:00:41,570 --> 00:00:48,410 ‫So I want to have this as an integer, but in this case we don't use a basic data type such as INT, 14 00:00:48,410 --> 00:00:50,870 ‫but we use a data type that we created ourselves. 15 00:00:51,290 --> 00:00:58,430 ‫So what I'm going to do now is I'm going to check if Ice Cube is not null, because if it is null, 16 00:00:58,430 --> 00:01:00,050 ‫then it's not a cube. 17 00:01:00,050 --> 00:01:02,510 ‫But if it's not null, then it will be a cube. 18 00:01:02,510 --> 00:01:14,000 ‫So this means if it is null, we can safely say this shape is no cube, all right, because it just 19 00:01:14,690 --> 00:01:18,980 ‫didn't work to add or put this into a cube. 20 00:01:20,340 --> 00:01:24,780 ‫Because well, if it's not in Cuba, then it will probably, in our case, be a sphere, right? 21 00:01:25,770 --> 00:01:35,470 ‫And then what I can also do is I can check if it is of a certain type, not just by using the double 22 00:01:35,470 --> 00:01:38,020 ‫equal sign and using null. 23 00:01:38,020 --> 00:01:42,250 ‫But I can actually check if the shape is of type cube. 24 00:01:43,150 --> 00:01:47,260 ‫And if that's the case, I can also safely say that this. 25 00:01:49,470 --> 00:01:51,570 ‫Is a cube. 26 00:01:53,230 --> 00:01:55,690 ‫Or this shape is a cube. 27 00:01:55,720 --> 00:01:56,400 ‫All right. 28 00:01:56,410 --> 00:02:02,140 ‫So we use the is keyword to check if a variable is of a certain type. 29 00:02:02,320 --> 00:02:07,090 ‫We use the as keyword in order to create a variable as a certain type. 30 00:02:07,120 --> 00:02:08,350 ‫It doesn't always work, though. 31 00:02:08,950 --> 00:02:09,340 ‫All right. 32 00:02:09,340 --> 00:02:16,740 ‫So now if we run this again, we will once see that this is a shape or the shape is is no cube. 33 00:02:16,750 --> 00:02:20,910 ‫And then we will see that this is a cube, because one of them is a cube, the other one is no cube. 34 00:02:20,920 --> 00:02:24,640 ‫So the first one is a sphere and it says the shape is no cube. 35 00:02:24,760 --> 00:02:26,770 ‫And the next one is this is a cube. 36 00:02:26,770 --> 00:02:28,300 ‫And it says the shape is a cube. 37 00:02:28,360 --> 00:02:28,720 ‫All right. 38 00:02:28,720 --> 00:02:32,740 ‫So now we can see that this actually worked, so we can actually check it. 39 00:02:32,740 --> 00:02:35,290 ‫And in this case, it's super stupid, right? 40 00:02:35,290 --> 00:02:38,920 ‫Because we're just checking it in order to write something onto the console. 41 00:02:38,920 --> 00:02:44,050 ‫But in other cases, it might actually be relevant for your program because then you can do something 42 00:02:44,050 --> 00:02:44,400 ‫with it. 43 00:02:44,410 --> 00:02:50,620 ‫If it is a cube, then you can do something particular, whether you can calculate it or you can adjust 44 00:02:50,620 --> 00:02:53,240 ‫it, or you can store it in a database or whatever. 45 00:02:53,260 --> 00:02:56,050 ‫If it's something else, then you would handle it differently. 46 00:02:56,160 --> 00:02:58,030 ‫Okay, so that's what you can do. 47 00:02:58,030 --> 00:02:58,840 ‫It's pretty cool. 48 00:02:58,880 --> 00:03:02,230 ‫It's part of polymorphism that we're using here. 49 00:03:02,380 --> 00:03:03,220 ‫That's pretty cool. 50 00:03:03,220 --> 00:03:06,590 ‫So you can see you have another functionality that you can use. 51 00:03:06,610 --> 00:03:11,440 ‫Now let's say we want to cast from an object to a cube. 52 00:03:11,620 --> 00:03:16,360 ‫This, of course, only works if the object, in fact, contains a cube. 53 00:03:16,390 --> 00:03:18,220 ‫All right, so let's create a cube. 54 00:03:18,220 --> 00:03:19,870 ‫I'm going to call this one cube one. 55 00:03:19,870 --> 00:03:25,330 ‫And it's going to be a new cube, and it's going to have a radius of seven. 56 00:03:25,570 --> 00:03:26,290 ‫All right. 57 00:03:26,290 --> 00:03:32,770 ‫So now what I can do is I can create a new cube, and I'm going to call this one cube two, which will 58 00:03:33,220 --> 00:03:34,570 ‫cast Cube. 59 00:03:36,230 --> 00:03:37,640 ‫One to a cube. 60 00:03:37,670 --> 00:03:38,960 ‫So what does that mean? 61 00:03:39,410 --> 00:03:47,990 ‫It means that I want to create a new cube called Cube two, and I want to cast Cube one into a cube. 62 00:03:48,170 --> 00:03:52,010 ‫This will, of course, only work if Cube one is, in fact, a cube. 63 00:03:52,250 --> 00:03:54,800 ‫And now we know that it is a cube. 64 00:03:54,800 --> 00:03:57,950 ‫But that's because we programmed it this way. 65 00:03:58,310 --> 00:04:00,260 ‫At the end, it's actually an object. 66 00:04:00,260 --> 00:04:03,140 ‫So it's an object which contains a cube. 67 00:04:03,320 --> 00:04:11,840 ‫So object is the class that is pretty much here supports all classes in the net class hierarchy and 68 00:04:11,840 --> 00:04:15,470 ‫provides low level services to derived classes. 69 00:04:15,500 --> 00:04:22,190 ‫This means that every single class is inheriting from this class called object every class. 70 00:04:22,370 --> 00:04:24,260 ‫And that's the case in C. 71 00:04:25,010 --> 00:04:28,490 ‫That's the case also in Java and other programming languages. 72 00:04:28,490 --> 00:04:30,220 ‫It's then probably any. 73 00:04:30,230 --> 00:04:35,300 ‫So for example, in Kotlin, it's a keyword any which is also the same kind of concept. 74 00:04:35,300 --> 00:04:35,840 ‫Right. 75 00:04:35,870 --> 00:04:42,290 ‫So this object class contains something like two string to hash and if you want to know more, you can 76 00:04:42,290 --> 00:04:43,190 ‫actually check it out. 77 00:04:43,190 --> 00:04:50,660 ‫So keep control key pressed and you can see it has equals reference equals gets hash code get typed 78 00:04:50,660 --> 00:04:51,500 ‫to string. 79 00:04:51,500 --> 00:04:56,330 ‫So these are the methods that every single object has, even though you haven't implemented it. 80 00:04:56,420 --> 00:05:00,590 ‫So you can very quickly see that by. 81 00:05:01,300 --> 00:05:05,380 ‫Using this for, let's say, our shape. 82 00:05:05,950 --> 00:05:06,910 ‫So here. 83 00:05:08,230 --> 00:05:10,830 ‫Shape start to string. 84 00:05:10,840 --> 00:05:16,870 ‫You can see it has this method same as shape dot equals get hashcode, get info and so forth. 85 00:05:16,870 --> 00:05:23,470 ‫So most of those properties that we can see here we haven't implemented, we just have them because 86 00:05:23,500 --> 00:05:25,690 ‫every class is inheriting from object. 87 00:05:26,590 --> 00:05:27,310 ‫All right. 88 00:05:29,790 --> 00:05:30,780 ‫So now. 89 00:05:32,720 --> 00:05:36,620 ‫Let's write something onto the console with our Q two that we have here. 90 00:05:36,620 --> 00:05:48,500 ‫So, C.W., and what I'm going to say is, has a volume of and then just going to pass or print the 91 00:05:48,500 --> 00:05:49,160 ‫volume. 92 00:05:49,160 --> 00:05:53,450 ‫So cube to name and cube to dot volume. 93 00:05:54,500 --> 00:05:55,070 ‫All right. 94 00:05:55,070 --> 00:05:57,080 ‫And this, of course, will only work. 95 00:05:58,710 --> 00:06:04,320 ‫If it actually is a cube, and in our case it is. 96 00:06:04,530 --> 00:06:06,840 ‫So it should work fine. 97 00:06:07,230 --> 00:06:08,250 ‫Now let's run it. 98 00:06:11,950 --> 00:06:16,810 ‫And we can see CUPE has a volume of 300 or 343. 99 00:06:17,050 --> 00:06:20,380 ‫So that's the cube that we created here, CUPE with seven. 100 00:06:21,100 --> 00:06:21,640 ‫All right. 101 00:06:21,640 --> 00:06:22,390 ‫So that's it. 102 00:06:22,420 --> 00:06:28,510 ‫Now, you have seen how to use abstract, how you can work with abstract classes, that when you create 103 00:06:28,510 --> 00:06:35,860 ‫an abstract class that you can of course, create abstract methods which you then have to implement 104 00:06:35,860 --> 00:06:38,760 ‫once a class inherits from this class. 105 00:06:38,770 --> 00:06:40,870 ‫So once a class inherits from shape. 106 00:06:41,700 --> 00:06:42,300 ‫All righty. 107 00:06:42,300 --> 00:06:43,650 ‫I hope you enjoyed this video. 108 00:06:43,680 --> 00:06:45,960 ‫This is another one of those videos where I got feedback. 109 00:06:45,960 --> 00:06:49,260 ‫People asked for it, so I decided to create this specific video. 110 00:06:49,260 --> 00:06:57,300 ‫And if you have any other demands or questions, please let me know in the question section or in discord.