C# Tutorial 9 Abstract Class Polymorphism

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 9 of my c-sharp video tutorial in this part of the tutorial we're going to cover abstract classes abstract methods based classes is as casting and a whole bunch more about polymorphism like always all the code is available in the description along with the transcript of this video and I have a lot to do so let's get into it okay so I created four classes here I have the program dot CS of course I'm going to have shaped CS circle dot CS and rectangle dot CS and you know how to create those usko project and add class now very often you're going to want to define a class that you do not want to instantiate and in that case an abstract class may be the way for you to go actually most of the time it would be and to make a class abstract it's very easy to say hey this is an abstract class and that is all you do and by instantiating I mean to create an object of a specific class type that's what I mean instantiating okay so let's come in here and this is my shape file of course and I'm going to go and define a name property inside of here and have it automatically generate my getters and my setters now inside of this I'm then going to create a whole bunch of different types of shapes see in this situation shape is like this abstract thing it doesn't make sense to instantiate it there is no such thing as an object that is a shape it's a vague thing which is sort of hints based off the name and that it should be abstract now you're also going to be able to define non abstract methods inside of your abstract class and what that means is the more abstract method is just the same as anything else so we can come in here and we'll say virtual and void and get info and what this guy's going to do let's go make this capital get info and this is just going to print out some information on the name that you assigned to it so we'll just go in console and write line and then we're going to come in and we're just saying something like this is a and throw in the name inside of there just to do something really simple and I'll do more complicated things with it later but you're also going to be able to come in here and define abstract methods now in this situation what we want to do is we want to be able to calculate the area of any type of shape and what that means is we want subclasses to override this method and to do so we are going to come in and Mark it also as abstract and it's going to return a double which is going to be the area and that is all you do and you put a semicolon at the end you don't put it any curly brackets or nothing and that is an abstract method that is basically created just to be overridden and that's all we're going to do we're going to keep this very very simple now I'm going to jump over to the circle file and let's create that class all right so if we want to come in here and inherit from shape and get all of its different fields and methods we just go in and do that just like we did previously and this is going to give me a little error message it's going to say circle does not implement inherited abstract method shape area C by marking a method as abstract that means you must use it so we're going to go like this and we're going to do like this and it automatically created it for me I'm also going to come in here however and I'm going to create the radius which we're going to need for our circle and let's just come in here and generate the getters and setters for that as well and let's also come in and generate a constructor so we'll go circle of course and this is going to receive a double which is going to be the radius and we're going to be able to reference name over in shape and we can just go name like this and we know that this is going to be a circle so we'll just put circle inside of there and then likewise we can also of course assign radius to whatever they passed inside of there so there we have that all stored now we need to come down here into area and you see it's going to throw this exception in the next part of the tutorial meant to cover exceptions in a lot more detail we're going to do here is calculate the area by referencing PI inside of the map library and then we'll go math and radius ^ - right like that and there we were able to calculate the area you're also going to be able to come in here and replace a method using override of course so we'll come in here and what I mean by that is I'm going to come in here - as you get info method and I'm going to show you how to override it and also have called base class so we're just going to come in Bango public and override just like you saw on previous tutorials void and then get info we're then going to be able to call the get info method for our base class just like that and then we can also come in here and let's say that we want to add some additional information as well so we can do that and that can just be something like it has a radius all and let's go and get whatever the radius is for it and without put all of that information okay so that's what we're going to do a circle we're just going to test all this stuff to see how it works after we get it all set up so now let's jump over into rectangle rectangle of course is not going to have a radius but instead it is going to have a length and width so we'll go and double and this is going to be lengths and slips setter and getter and we're going to do exactly the same thing for width so it must copy that and change this to width and don't forget that we are going to be inheriting from shape so let's go in there and throw that inside of there and let's spell shape correct there that is and it's going to give me an error message I'm going to say show potential fixes and I'm just going to say implement that and there's our area of course we're going to have to create a special constructor for our rectangle and that's just going to be rectangle and this of course is going to receive a link as well as a height or a width I mean throw that inside of there and then let's go and assign these as well so we'll go length is equal to the link that was passed inside of there and then the width is of course going to be whatever the width was that was passed inside of that and also we want to come in and assign name a value and the name is going to be rectangle like that all right so we have the constructor all set up and we have this guy right here the area Salou might as well just come in here and handle this already we'll just go return and this is going to be length times the width all set up and then let's go in there and let's also replace get info with something specific to our rectangles and to do that we're going to go public and override void and get info and let's call the base for this guy's well to have that information get printed out and then let's print out something that is specific to rectangles so we'll say that the it has a length of and we'll get the length and width of and we'll get the width and print that out all right so we have those all set up now what we're going to be able to do is jump over into program and here's program and play around with all this stuff and see how the abstract classes enhances our ability to take advantage of polymorphism now what we're going to be able to do here is store our shapes in a shape array as long as it contains subclasses of shape so to do so we can go shoot and there's our shape array and let's just call it shapes is equal to and it will go and create a circle and a rectangle to do so circle and then we will throw a 5 inside of there and then we will go new rectangle and inside of there we can do something like 4 and 5 so there we went and created a shape array with a circle and a rectangle inside of it now what we'll be able to do is come in and cycle through these shapes and then print on information on them so we can say for each and shape s in shapes and we can call the overridden method that we have so we can say something like get info there that is likewise we're going to be able to get the area so we can go console and write line and this time I'm not going to use stringent or interpol iation i'm going to come in and just do this and area that's going to put the name there and then i'm going to put in the actual area and i want this just to be two decimal places in length just to keep it nice and simple and then from that I can go s and get the name and then likewise I can go s and get our area and remember it's methods we have to put that there the parentheses okay so we got some good information going on here and I spelled shape wrong again what is wrong with me there we are now that's fixed the things kind of neat is you're going to be able to use the keyword as to check if an object is of a specific type so let's come in here and we'll go circle test circ is equal to s as circle and it's either going to get a value or it is not going to get a value based off of that what we can do is then after this is say if test circ is equal to null in this situation then we know that this isn't a circle so we'll console.writeline and then we'll just do a simple message like this isn't a circle likewise there is another keyword and that keyword is is and we can do something like if s is a circle and in this situation we can come in and I'll put some information on our screen and in that situation we're going to put this isms a rectangle just to do something different and just to add a little bit of flavor here let's go and copy this as well so that this looks a little bit nicer throw a new line in there after that output and it's also important to know that you're going to be able to store any class as a base type and on top of that you're going to be able to call the subclass methods even if they don't exist in the base but what you're going to have to do is cast what I mean lines let's go and use the most base type which is object which is what everything comes from and we'll be able to come in here and let's just go to Cirque one and let's create a circle now this normally would not work because we can only normally ever call functions or methods or whatever that exists inside of the base type in this situation however if you know that this is a circle you can cast and make it work how you can do that is just go circle and search two is equal to and then cast it into being a circle and it will go sir one like that and here you're going to be able to see that indeed that is going to work for us and here we can just say the area is and let's make this only two decimal places just like we did before and then after that we can go circ to name and then circ two and we will call the area function and then after we do that we can run this and we can test that everything is working and start and you're going to see that went in there said this is a circle and you're going to be able to see if we move this out of the way right here where we are calling get info and the get info woods actually let's go in here look at this here's get info it called the base which is actually the shape area the shape version of get info and it printed out the name and then likewise it has a radius of five where did that come from I came from this guy it has a radius of five we then called the area and it properly calculated that now putted it and we then used is and as to figure out if they were rectangles or if they were circles and then finally we also came in here and calculated the circle area even though it was originally saved as a object type and that guy is right here say we say this is an object and then through the use of casting we were still able to call the proper method alright so there's a rough overview of abstract classes abstract methods and how we can cast and another deeper look at polymorphism as well as casting and a whole bunch of other different things and like always please leave your questions and comments below of the last till next time
Info
Channel: Derek Banas
Views: 62,235
Rating: undefined out of 5
Keywords: C# Abstract Classes, C# Virtual Methods, C# Override, C# Polymorphism
Id: RaHm_pSxqLY
Channel Id: undefined
Length: 13min 24sec (804 seconds)
Published: Sat Feb 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.