1 00:00:00,540 --> 00:00:01,560 ‫Welcome back. 2 00:00:01,590 --> 00:00:07,590 ‫In this video, we are going to cover abstract classes and therefore we are going to create a bunch 3 00:00:07,590 --> 00:00:08,250 ‫of new classes. 4 00:00:08,250 --> 00:00:14,640 ‫And it's important that you understand the concept of classes and also inheritance and polymorphism, 5 00:00:14,640 --> 00:00:16,560 ‫because we are going to build on that. 6 00:00:16,560 --> 00:00:20,100 ‫And what we start with is to create a new class. 7 00:00:20,100 --> 00:00:21,540 ‫So let's create a new class. 8 00:00:21,540 --> 00:00:30,480 ‫Here we go to Project Add class, and I'm going to create a shape class and that's going to be the shape 9 00:00:30,570 --> 00:00:31,190 ‫CSS. 10 00:00:31,200 --> 00:00:38,220 ‫Now, the idea is that we want to create a class of which we don't want to create any objects, so we 11 00:00:38,220 --> 00:00:41,220 ‫won't don't want to instantiate shapes. 12 00:00:41,220 --> 00:00:47,940 ‫We want to instantiate objects which are types of shapes but not shapes himself. 13 00:00:47,940 --> 00:00:55,350 ‫So for example, a shape would be a cube or a shape would be a sphere. 14 00:00:55,350 --> 00:00:57,570 ‫And I'm using 3D shapes here, right? 15 00:00:57,570 --> 00:00:59,220 ‫Three dimensional shapes. 16 00:00:59,220 --> 00:01:06,690 ‫And well, we are going to create classes which will inherit from shape, but we want to make sure that 17 00:01:06,690 --> 00:01:13,500 ‫shape is an actual abstract class, which means that we won't be able to create an object of it, but 18 00:01:13,500 --> 00:01:19,470 ‫we will use its systems or its methods and its properties. 19 00:01:19,470 --> 00:01:20,850 ‫So let's get started. 20 00:01:21,270 --> 00:01:23,910 ‫Let's make this shape abstract, first of all. 21 00:01:23,910 --> 00:01:27,030 ‫And in order to do so, we can use the keyword abstract. 22 00:01:27,030 --> 00:01:29,640 ‫So now it's an abstract class. 23 00:01:30,800 --> 00:01:36,410 ‫Now let's go ahead to our program that sees and see that it is an actual abstract class. 24 00:01:36,410 --> 00:01:37,840 ‫So let's create a shape. 25 00:01:37,850 --> 00:01:41,540 ‫Shape one equals new shape. 26 00:01:43,070 --> 00:01:49,250 ‫So when we try to do that, you can see that cannot create an instance of an abstract class or interface 27 00:01:49,250 --> 00:01:49,830 ‫shape. 28 00:01:49,850 --> 00:01:58,280 ‫And as our shape is an abstract class, we cannot create objects of it, so we cannot instantiate it. 29 00:01:58,310 --> 00:01:59,960 ‫That's an important aspect here. 30 00:02:00,230 --> 00:02:00,530 ‫All right. 31 00:02:00,530 --> 00:02:06,410 ‫Now we will need a property, and I'm just going to create this property called name. 32 00:02:07,370 --> 00:02:15,050 ‫So the shape needs a name and we have a getter, etc. In order to create this, I'm just using pre prop 33 00:02:15,050 --> 00:02:18,440 ‫and then double tab in order to create a new property. 34 00:02:18,770 --> 00:02:21,370 ‫So we've seen that in other videos. 35 00:02:21,380 --> 00:02:29,240 ‫Now I want to have a method and the method that I want to use will be a virtual method. 36 00:02:29,240 --> 00:02:31,640 ‫Virtual void get info. 37 00:02:33,250 --> 00:02:39,670 ‫This virtual keyboard that you see here is used to modify a method property indexer or event declared 38 00:02:39,670 --> 00:02:44,260 ‫in the base class and allows to be overwritten in the derived class. 39 00:02:44,710 --> 00:02:50,440 ‫So this allows us to override the get info method in a driving class. 40 00:02:50,470 --> 00:02:50,860 ‫All right. 41 00:02:50,860 --> 00:02:53,650 ‫So that's the idea behind this virtual keyword here. 42 00:02:55,330 --> 00:02:57,610 ‫So let's just add a statement here. 43 00:02:57,610 --> 00:03:01,120 ‫So see w double tab in order to get controlled right line. 44 00:03:01,120 --> 00:03:05,470 ‫And here I just want to write something like. 45 00:03:07,220 --> 00:03:11,420 ‫This is a and then the name of the shape. 46 00:03:11,690 --> 00:03:13,570 ‫So this is a name. 47 00:03:13,580 --> 00:03:21,590 ‫And now the idea behind this is that I want to have a new line and then I want to have this statement. 48 00:03:21,590 --> 00:03:26,390 ‫This is a and then the name of the shape, because whenever we create a shape, we can set the name 49 00:03:26,390 --> 00:03:27,080 ‫of it. 50 00:03:27,260 --> 00:03:28,820 ‫So we have a virtual void. 51 00:03:28,850 --> 00:03:32,640 ‫Now, now we can also add an abstract method. 52 00:03:32,660 --> 00:03:38,080 ‫Now let's do that abstract double volume. 53 00:03:38,090 --> 00:03:42,860 ‫So what I want to calculate is the volume of a shape. 54 00:03:42,890 --> 00:03:46,850 ‫Now, here, I will not implement this method. 55 00:03:46,880 --> 00:03:48,830 ‫That's an important distinction here. 56 00:03:48,830 --> 00:03:50,600 ‫I'm not going to implement it here. 57 00:03:50,600 --> 00:03:59,150 ‫I'm just going to say, okay, whenever a class wants to inherit from me, it needs to implement a functionality 58 00:03:59,150 --> 00:04:01,580 ‫for the volume method. 59 00:04:01,850 --> 00:04:02,410 ‫All right. 60 00:04:02,420 --> 00:04:06,620 ‫Now, we could, of course, call this one, calculate volume or stuff like that, but I'm just going 61 00:04:06,620 --> 00:04:07,910 ‫to call this one volume. 62 00:04:08,360 --> 00:04:08,660 ‫All right. 63 00:04:08,660 --> 00:04:14,540 ‫So now that we have our shape class prepared, we can save it and we can create a new class. 64 00:04:14,540 --> 00:04:17,510 ‫So the next class that I'm going to create is the cube class. 65 00:04:17,510 --> 00:04:21,260 ‫So I'm just going to create Cube, which is a 3D shape. 66 00:04:21,290 --> 00:04:25,400 ‫So now my cube will inherit from shape. 67 00:04:25,940 --> 00:04:28,970 ‫Now, if I do so, you can see I get an error here. 68 00:04:28,970 --> 00:04:34,460 ‫It says Cube does not implement inherited abstract member shape volume. 69 00:04:34,670 --> 00:04:36,200 ‫So show potential fix this. 70 00:04:36,200 --> 00:04:38,060 ‫All right, implement abstract class. 71 00:04:38,060 --> 00:04:45,140 ‫If I do so you can see it implements this volume method with an override statement. 72 00:04:45,140 --> 00:04:49,460 ‫So it says it's overriding whatever was defined in shape. 73 00:04:49,460 --> 00:04:52,850 ‫And as shape is an abstract class and this method is abstract. 74 00:04:53,360 --> 00:04:56,240 ‫As you can see, there is no method body. 75 00:04:56,270 --> 00:04:58,750 ‫It's really just the declaration of the method. 76 00:04:58,760 --> 00:05:02,060 ‫So now we can go ahead and of course implement something. 77 00:05:02,060 --> 00:05:05,360 ‫You can see we throw an error here, not implemented exception. 78 00:05:05,360 --> 00:05:07,550 ‫Of course we want to implement something in here. 79 00:05:07,550 --> 00:05:12,770 ‫So let's implement a volume calculation in our cube class. 80 00:05:14,030 --> 00:05:22,400 ‫Now in order to calculate it, what we will need is we will need a property which is going to be a length. 81 00:05:22,700 --> 00:05:28,730 ‫So I'm just going to create this new property called length here, and I'm also going to add a constructor. 82 00:05:28,910 --> 00:05:38,150 ‫So Public Cube and whenever I want to create a cube, I need to pass a length. 83 00:05:38,150 --> 00:05:40,130 ‫So that's what I'm staying saying here. 84 00:05:40,130 --> 00:05:43,970 ‫And while I'm at it, I'm also going to add a name. 85 00:05:43,970 --> 00:05:50,420 ‫So as you can see, we're using inheritance here because name is not defined in cube, but it is defined 86 00:05:50,420 --> 00:05:51,230 ‫in shape. 87 00:05:51,890 --> 00:05:57,740 ‫So I can just set the variable name here and I'm going to set the length to the length that was passed 88 00:05:57,740 --> 00:05:58,370 ‫to me. 89 00:05:58,640 --> 00:06:04,760 ‫So the property length will be the same length as the length that is passed once we create an object 90 00:06:04,760 --> 00:06:05,120 ‫of type. 91 00:06:05,120 --> 00:06:07,400 ‫Q So that's the idea behind this variable. 92 00:06:07,400 --> 00:06:13,730 ‫And next we can implement this volume method and we can simply return something. 93 00:06:13,730 --> 00:06:15,710 ‫So it needs to return a double, right? 94 00:06:15,710 --> 00:06:19,070 ‫So we need to return something that is based on double. 95 00:06:19,070 --> 00:06:27,290 ‫And in order to calculate the volume of a cube and hereby I'm using a cube which has the same length 96 00:06:27,290 --> 00:06:28,520 ‫on all sides. 97 00:06:28,520 --> 00:06:32,090 ‫So I'm just going to say length to the power of three. 98 00:06:32,090 --> 00:06:38,480 ‫So math, pulp, length three, that means that it's going to calculate length, times length, times 99 00:06:38,480 --> 00:06:39,080 ‫length. 100 00:06:39,590 --> 00:06:43,970 ‫So this is what our volume will be and it will return this value. 101 00:06:44,000 --> 00:06:49,790 ‫Now we can go ahead and we can also implement this method called get info. 102 00:06:49,790 --> 00:06:56,870 ‫So what I'm going to do is I'm just going to copy it and I'm going to now not use the virtual keyword, 103 00:06:56,870 --> 00:07:05,000 ‫but override keyword because I want to override the get info method from my parent now and here. 104 00:07:05,000 --> 00:07:14,600 ‫If I do so what I can do is I can also call the get info method from my base class, which means in 105 00:07:14,600 --> 00:07:19,610 ‫this case it's also going to call get info from shape dot CSS. 106 00:07:20,150 --> 00:07:22,430 ‫So it's going to call this method here. 107 00:07:22,460 --> 00:07:28,820 ‫So meaning that will execute this code and then it will execute whatever code we add at the bottom of 108 00:07:28,820 --> 00:07:29,150 ‫it. 109 00:07:29,390 --> 00:07:39,620 ‫So I'm going to add a little text here and it's going to say the cube has a length of and then I'm going 110 00:07:39,620 --> 00:07:41,060 ‫to pass the length here. 111 00:07:41,660 --> 00:07:47,480 ‫So length, of course, I could also calculate the volume here, but I'm going to leave that to you. 112 00:07:47,480 --> 00:07:49,430 ‫So if you want to do it, feel free. 113 00:07:49,730 --> 00:07:50,150 ‫All right. 114 00:07:50,150 --> 00:07:52,040 ‫So this is how we can. 115 00:07:52,800 --> 00:07:59,190 ‫Go ahead and create a class that is going to inherit from one other class that is abstract. 116 00:08:00,780 --> 00:08:05,630 ‫Now, as a little challenge for you and I'm just going to pause the video once, you should start with 117 00:08:05,640 --> 00:08:06,540 ‫a challenge. 118 00:08:06,540 --> 00:08:14,460 ‫So the idea is now create another shape, which is another class which is inheriting from shape, and 119 00:08:14,460 --> 00:08:16,920 ‫the shape should be a sphere. 120 00:08:17,190 --> 00:08:22,500 ‫Now implement the same type of functionality that you can see here for Sphere. 121 00:08:23,770 --> 00:08:24,310 ‫All right. 122 00:08:24,460 --> 00:08:24,880 ‫Good luck. 123 00:08:24,880 --> 00:08:26,650 ‫Post the video and try to do that. 124 00:08:27,730 --> 00:08:28,060 ‫All right. 125 00:08:28,060 --> 00:08:29,190 ‫So I hope you tried it. 126 00:08:29,200 --> 00:08:35,230 ‫I'm going to create a new class here and I'm going to call this class sphere dot CSS. 127 00:08:36,160 --> 00:08:40,300 ‫And this class should be inheriting from shape. 128 00:08:42,330 --> 00:08:49,410 ‫And of course, I need to implement my abstract class, which means that I need to implement this volume 129 00:08:49,410 --> 00:08:49,980 ‫method. 130 00:08:50,100 --> 00:08:58,800 ‫Now in here I can of course, again calculate the or have a property, but this time a useful property 131 00:08:58,800 --> 00:09:04,020 ‫would not be the length, but it would actually be the radius, because we calculate the volume differently 132 00:09:04,020 --> 00:09:06,450 ‫in the sphere than we do in a cube. 133 00:09:06,450 --> 00:09:14,580 ‫And we didn't do it in shape because a shape by itself cannot have a volume right? 134 00:09:14,580 --> 00:09:20,490 ‫If it's not defined as a Y, we cannot calculate the volume of it, let's say, because we don't know 135 00:09:20,490 --> 00:09:21,390 ‫what kind of shape it is. 136 00:09:21,390 --> 00:09:21,830 ‫Right. 137 00:09:21,840 --> 00:09:27,720 ‫That's why we need those classes which inherit from shape and which implement the functionality. 138 00:09:27,960 --> 00:09:35,250 ‫So now I can go ahead and create a constructor, public sphere, a double radius. 139 00:09:35,250 --> 00:09:43,110 ‫So we need a radius and then we can go ahead and set the name of the sphere, which should be sphere, 140 00:09:43,740 --> 00:09:48,660 ‫and we should have a radius property value. 141 00:09:48,660 --> 00:09:54,150 ‫So we assign the radius that is passed to us as the property radius value. 142 00:09:54,390 --> 00:09:57,510 ‫All right, now that we have that, we can calculate the volume. 143 00:09:57,510 --> 00:10:04,440 ‫So now instead of throwing an exception here, I want to calculate the volume of my sphere. 144 00:10:04,440 --> 00:10:10,740 ‫And in order to do so, I need PI and I need the following calculation. 145 00:10:10,740 --> 00:10:21,600 ‫So math that pal radius to the power three and all of that multiplied with three divided by oh four 146 00:10:21,630 --> 00:10:22,560 ‫divided by three. 147 00:10:22,980 --> 00:10:23,280 ‫All right. 148 00:10:23,280 --> 00:10:30,120 ‫So this is how we calculate the volume, and now we can go ahead and also implement the get info method. 149 00:10:30,120 --> 00:10:34,560 ‫So I'm just going to copy get info from my cube. 150 00:10:35,220 --> 00:10:36,240 ‫I'm going to be lazy. 151 00:10:36,240 --> 00:10:40,560 ‫So copy this part here and implement the same thing. 152 00:10:40,560 --> 00:10:46,170 ‫So we use the override keyword again, it's exactly the same name as it was in the shape class. 153 00:10:46,170 --> 00:10:52,740 ‫And we use base code info here in order to call whatever was the method in here. 154 00:10:52,740 --> 00:10:59,460 ‫So whatever was the method body in there and then instead of passing length or saying something about 155 00:10:59,460 --> 00:11:04,080 ‫the length, we are going to say something about the radius and the cube. 156 00:11:04,080 --> 00:11:09,210 ‫In this case, it's the sphere has a radius of radius. 157 00:11:10,050 --> 00:11:10,410 ‫All right. 158 00:11:10,410 --> 00:11:16,440 ‫Now that we have all of that, what we can now do is we can go ahead and create objects of all of those 159 00:11:16,440 --> 00:11:17,970 ‫different classes. 160 00:11:17,970 --> 00:11:20,190 ‫So I'm not going to say hello world here. 161 00:11:20,370 --> 00:11:24,180 ‫Instead, I'm going to create a bunch of shapes. 162 00:11:24,180 --> 00:11:28,350 ‫So I'm going to say, okay, please give me an array of shapes. 163 00:11:28,740 --> 00:11:39,180 ‫And it's going to be a new sphere with a radius of four and a new cube with a radius of three. 164 00:11:40,260 --> 00:11:44,640 ‫And in this case, actually, it's the length of three because Cube doesn't have a radius, it has a 165 00:11:44,640 --> 00:11:45,180 ‫length. 166 00:11:45,420 --> 00:11:50,490 ‫And then I want to execute something for each of the shapes. 167 00:11:50,490 --> 00:11:55,740 ‫So for shape in shapes, I want to. 168 00:11:56,490 --> 00:11:59,190 ‫Go through all of all of them in this case, just the two of them. 169 00:11:59,190 --> 00:12:02,550 ‫But what I want to have is shape the info. 170 00:12:02,550 --> 00:12:10,140 ‫So get info should be called and I want to write something like this. 171 00:12:10,980 --> 00:12:13,320 ‫And in this case, zero. 172 00:12:14,950 --> 00:12:17,860 ‫Will be the name and then I want to show the volume. 173 00:12:17,860 --> 00:12:23,530 ‫So I want to have the name has a volume of. 174 00:12:24,230 --> 00:12:28,400 ‫And then, of course, I want to know the volume of this piece that we're looking at. 175 00:12:28,400 --> 00:12:32,120 ‫So shape the name and shape the volume. 176 00:12:33,950 --> 00:12:37,580 ‫So here it's important that we call the volume method, right? 177 00:12:37,580 --> 00:12:39,470 ‫It's not just a. 178 00:12:40,290 --> 00:12:41,130 ‫Property. 179 00:12:41,730 --> 00:12:48,630 ‫So we have console red line has a volume of in this case name has a volume of volume. 180 00:12:48,870 --> 00:12:51,260 ‫So it will just calculate the volume. 181 00:12:51,270 --> 00:12:53,790 ‫Okay, so now let's just test this. 182 00:12:53,940 --> 00:13:00,330 ‫Let's just see what's going on here, what we have created and that we are so we have an empty, empty 183 00:13:00,330 --> 00:13:01,290 ‫line here. 184 00:13:01,320 --> 00:13:02,760 ‫Then it says, this is a sphere. 185 00:13:02,760 --> 00:13:04,140 ‫The sphere has a radius of four. 186 00:13:04,170 --> 00:13:07,470 ‫The sphere has a volume of 268.08. 187 00:13:07,770 --> 00:13:09,150 ‫And then this is a cube. 188 00:13:09,150 --> 00:13:10,800 ‫This cube has a length of three. 189 00:13:10,800 --> 00:13:13,110 ‫The cube has a volume of 27. 190 00:13:13,380 --> 00:13:13,710 ‫All right. 191 00:13:13,710 --> 00:13:21,660 ‫So you can see that this is a very simple code that we wrote here, but it already explains or shows 192 00:13:21,660 --> 00:13:25,140 ‫most of the functionality that we need to know about abstract classes. 193 00:13:25,350 --> 00:13:30,990 ‫Now, the cool thing is we can also use some more keywords that we haven't used yet. 194 00:13:31,320 --> 00:13:35,580 ‫And as this video is getting long enough, I'd say we do that in the next video. 195 00:13:35,580 --> 00:13:36,750 ‫So see you there.