‫Welcome back.
‫In this video, we are going to cover abstract classes and therefore we are going to create a bunch
‫of new classes.
‫And it's important that you understand the concept of classes and also inheritance and polymorphism,
‫because we are going to build on that.
‫And what we start with is to create a new class.
‫So let's create a new class.
‫Here we go to Project Add class, and I'm going to create a shape class and that's going to be the shape
‫CSS.
‫Now, the idea is that we want to create a class of which we don't want to create any objects, so we
‫won't don't want to instantiate shapes.
‫We want to instantiate objects which are types of shapes but not shapes himself.
‫So for example, a shape would be a cube or a shape would be a sphere.
‫And I'm using 3D shapes here, right?
‫Three dimensional shapes.
‫And well, we are going to create classes which will inherit from shape, but we want to make sure that
‫shape is an actual abstract class, which means that we won't be able to create an object of it, but
‫we will use its systems or its methods and its properties.
‫So let's get started.
‫Let's make this shape abstract, first of all.
‫And in order to do so, we can use the keyword abstract.
‫So now it's an abstract class.
‫Now let's go ahead to our program that sees and see that it is an actual abstract class.
‫So let's create a shape.
‫Shape one equals new shape.
‫So when we try to do that, you can see that cannot create an instance of an abstract class or interface
‫shape.
‫And as our shape is an abstract class, we cannot create objects of it, so we cannot instantiate it.
‫That's an important aspect here.
‫All right.
‫Now we will need a property, and I'm just going to create this property called name.
‫So the shape needs a name and we have a getter, etc. In order to create this, I'm just using pre prop
‫and then double tab in order to create a new property.
‫So we've seen that in other videos.
‫Now I want to have a method and the method that I want to use will be a virtual method.
‫Virtual void get info.
‫This virtual keyboard that you see here is used to modify a method property indexer or event declared
‫in the base class and allows to be overwritten in the derived class.
‫So this allows us to override the get info method in a driving class.
‫All right.
‫So that's the idea behind this virtual keyword here.
‫So let's just add a statement here.
‫So see w double tab in order to get controlled right line.
‫And here I just want to write something like.
‫This is a and then the name of the shape.
‫So this is a name.
‫And now the idea behind this is that I want to have a new line and then I want to have this statement.
‫This is a and then the name of the shape, because whenever we create a shape, we can set the name
‫of it.
‫So we have a virtual void.
‫Now, now we can also add an abstract method.
‫Now let's do that abstract double volume.
‫So what I want to calculate is the volume of a shape.
‫Now, here, I will not implement this method.
‫That's an important distinction here.
‫I'm not going to implement it here.
‫I'm just going to say, okay, whenever a class wants to inherit from me, it needs to implement a functionality
‫for the volume method.
‫All right.
‫Now, we could, of course, call this one, calculate volume or stuff like that, but I'm just going
‫to call this one volume.
‫All right.
‫So now that we have our shape class prepared, we can save it and we can create a new class.
‫So the next class that I'm going to create is the cube class.
‫So I'm just going to create Cube, which is a 3D shape.
‫So now my cube will inherit from shape.
‫Now, if I do so, you can see I get an error here.
‫It says Cube does not implement inherited abstract member shape volume.
‫So show potential fix this.
‫All right, implement abstract class.
‫If I do so you can see it implements this volume method with an override statement.
‫So it says it's overriding whatever was defined in shape.
‫And as shape is an abstract class and this method is abstract.
‫As you can see, there is no method body.
‫It's really just the declaration of the method.
‫So now we can go ahead and of course implement something.
‫You can see we throw an error here, not implemented exception.
‫Of course we want to implement something in here.
‫So let's implement a volume calculation in our cube class.
‫Now in order to calculate it, what we will need is we will need a property which is going to be a length.
‫So I'm just going to create this new property called length here, and I'm also going to add a constructor.
‫So Public Cube and whenever I want to create a cube, I need to pass a length.
‫So that's what I'm staying saying here.
‫And while I'm at it, I'm also going to add a name.
‫So as you can see, we're using inheritance here because name is not defined in cube, but it is defined
‫in shape.
‫So I can just set the variable name here and I'm going to set the length to the length that was passed
‫to me.
‫So the property length will be the same length as the length that is passed once we create an object
‫of type.
‫Q So that's the idea behind this variable.
‫And next we can implement this volume method and we can simply return something.
‫So it needs to return a double, right?
‫So we need to return something that is based on double.
‫And in order to calculate the volume of a cube and hereby I'm using a cube which has the same length
‫on all sides.
‫So I'm just going to say length to the power of three.
‫So math, pulp, length three, that means that it's going to calculate length, times length, times
‫length.
‫So this is what our volume will be and it will return this value.
‫Now we can go ahead and we can also implement this method called get info.
‫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,
‫but override keyword because I want to override the get info method from my parent now and here.
‫If I do so what I can do is I can also call the get info method from my base class, which means in
‫this case it's also going to call get info from shape dot CSS.
‫So it's going to call this method here.
‫So meaning that will execute this code and then it will execute whatever code we add at the bottom of
‫it.
‫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
‫to pass the length here.
‫So length, of course, I could also calculate the volume here, but I'm going to leave that to you.
‫So if you want to do it, feel free.
‫All right.
‫So this is how we can.
‫Go ahead and create a class that is going to inherit from one other class that is abstract.
‫Now, as a little challenge for you and I'm just going to pause the video once, you should start with
‫a challenge.
‫So the idea is now create another shape, which is another class which is inheriting from shape, and
‫the shape should be a sphere.
‫Now implement the same type of functionality that you can see here for Sphere.
‫All right.
‫Good luck.
‫Post the video and try to do that.
‫All right.
‫So I hope you tried it.
‫I'm going to create a new class here and I'm going to call this class sphere dot CSS.
‫And this class should be inheriting from shape.
‫And of course, I need to implement my abstract class, which means that I need to implement this volume
‫method.
‫Now in here I can of course, again calculate the or have a property, but this time a useful property
‫would not be the length, but it would actually be the radius, because we calculate the volume differently
‫in the sphere than we do in a cube.
‫And we didn't do it in shape because a shape by itself cannot have a volume right?
‫If it's not defined as a Y, we cannot calculate the volume of it, let's say, because we don't know
‫what kind of shape it is.
‫Right.
‫That's why we need those classes which inherit from shape and which implement the functionality.
‫So now I can go ahead and create a constructor, public sphere, a double radius.
‫So we need a radius and then we can go ahead and set the name of the sphere, which should be sphere,
‫and we should have a radius property value.
‫So we assign the radius that is passed to us as the property radius value.
‫All right, now that we have that, we can calculate the volume.
‫So now instead of throwing an exception here, I want to calculate the volume of my sphere.
‫And in order to do so, I need PI and I need the following calculation.
‫So math that pal radius to the power three and all of that multiplied with three divided by oh four
‫divided by three.
‫All right.
‫So this is how we calculate the volume, and now we can go ahead and also implement the get info method.
‫So I'm just going to copy get info from my cube.
‫I'm going to be lazy.
‫So copy this part here and implement the same thing.
‫So we use the override keyword again, it's exactly the same name as it was in the shape class.
‫And we use base code info here in order to call whatever was the method in here.
‫So whatever was the method body in there and then instead of passing length or saying something about
‫the length, we are going to say something about the radius and the cube.
‫In this case, it's the sphere has a radius of radius.
‫All right.
‫Now that we have all of that, what we can now do is we can go ahead and create objects of all of those
‫different classes.
‫So I'm not going to say hello world here.
‫Instead, I'm going to create a bunch of shapes.
‫So I'm going to say, okay, please give me an array of shapes.
‫And it's going to be a new sphere with a radius of four and a new cube with a radius of three.
‫And in this case, actually, it's the length of three because Cube doesn't have a radius, it has a
‫length.
‫And then I want to execute something for each of the shapes.
‫So for shape in shapes, I want to.
‫Go through all of all of them in this case, just the two of them.
‫But what I want to have is shape the info.
‫So get info should be called and I want to write something like this.
‫And in this case, zero.
‫Will be the name and then I want to show the volume.
‫So I want to have the name has a volume of.
‫And then, of course, I want to know the volume of this piece that we're looking at.
‫So shape the name and shape the volume.
‫So here it's important that we call the volume method, right?
‫It's not just a.
‫Property.
‫So we have console red line has a volume of in this case name has a volume of volume.
‫So it will just calculate the volume.
‫Okay, so now let's just test this.
‫Let's just see what's going on here, what we have created and that we are so we have an empty, empty
‫line here.
‫Then it says, this is a sphere.
‫The sphere has a radius of four.
‫The sphere has a volume of 268.08.
‫And then this is a cube.
‫This cube has a length of three.
‫The cube has a volume of 27.
‫All right.
‫So you can see that this is a very simple code that we wrote here, but it already explains or shows
‫most of the functionality that we need to know about abstract classes.
‫Now, the cool thing is we can also use some more keywords that we haven't used yet.
‫And as this video is getting long enough, I'd say we do that in the next video.
‫So see you there.