C# Programming Tutorials: Beginners 13 Abstract Classes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so what are abstract classes they used to describe a concept a partial implementation or something that's not completely known but as powerful known aspects so good examples of car the intimated the presumption that all cars with Drive using a gas pedal they'll all off the same radio and same handbrake or each car will have a different way to move the seats and windows will calculate their speed differently based on the wheel size next we'll need to implement two versions of the abstract class so we're going to take a budget car in a sports car they'll both automatically inherit the implemented methods of the abstract class so we don't need to be easily done next they lead to implement their own version of the abstract methods to move their seats and windows and calculate their speed the sports car will have electric seats and windows and the budget coral of manual adjustment the sports car has bigger wheels and a larger engine so travels faster the green methods are the methods that the abstract class influence and identical for all classes the black methods are obstruct unimplemented methods in the abstract class that get implemented in blue in each cluster inherit from the abstract class so a unique to each class okay so let's make this actual abstract class and budget car and sports card to see how this thing really comes together so just started a new console application and in here we're just going to do it all in one class for now keep it simple so let's start by making a substrate class so you make a class as you always would like this a class car and there's your your class and just simply prepend it with abstract so now an abstract class cannot be created so if we went in here now and Ed draw a equals new car you're going to find that you can't do this because it's abstract so that's the point of abstract issues it's not fully defined so you can't create an instance of it because there's unknown aspects so we now have an abstract class called car and as we mentioned we're going to make the presumption that all cars have the radio turned on the same way so I'll just make a function so probably void to radio on and then we'll just what we'll do for this is just simply console the right line for everything and we'll simply say turn the radio on and there's a function now in an abstract class or in any class really you might want to allow the classes that then subclass this to override and make new versions of this function so by default as we mentioned all of the classes that inherit from car will get this function by defaults they won't have to implement it but then they might want to specifically say upgrade the radio and then they want to turn it on differently so in order to allow this to be changed in future classes the pre thermos with the word virtual and you'll see this once we finish the lesson you'll see how we do that so virtual allows say a public class or G car which implements car - now do override radio on and then here we can either call the original function or we can completely replace it with our own function and do something different so I'll come to that in a minute but that is that's right allows you to do so then whoever calls the you know the function you can call the the specific one so we've got the radio on and we'll quickly wrap this in a region four and public methods keep things clean and then we'll even comment so an abstract definition of a car of any kind turn the car radio on and then we said also the handbrake will be released the same way for all vehicles so again we'll make the public virtual function boys says handbrake off so or what's rather release handbrake makes more sense attempts to release the cars and brake and then in order to do that we need a property so let's just make a properties region and here we'll put a public boolean handbrake released and by default the cars going on the handbrake on so it doesn't released so then in order to release the handbrake for all cars that were saying by default it's the same we would then do handbrake released it was real simple as that and then we would mention that we managed to do that so we now the radio on and the released handbrake functions implemented and the last one we said was in order to drive which is to press the gas pedal and drive so again a public virtual void of a drive function those on the gas to drive the car and then here now and again remember this is all demo good I don't typically like to do demos this way wait so you know a fake thing that isn't really in programming but this is just a really good way to explain abstract classes then I'll show you some real world examples again of where it's actually use but this is a a good way to explain the concept so the point of this as these three functions are going to be the same for all cars and then yes we can't override them with virtual to make them specific but at least it's you know if it's not implemented its class that this car will have the same you know way of doing that so same for like a handbrake you you release them the same in every car and then when you get to luxury cars you might have an electronic can brake so it kind of follow suit but it's easier to grasp this way so for the drive in order to drive we first need to release the handbrake so we'll say release um break the fish can't drive if you have brakes on before you can but your burn your brakes out and then we'll do a check with if the handbrake is released then we'll be driving so we can then go on the gas so let's say press the gas pedal otherwise if the handbrake didn't release then we'll say cannot move and brake jammed and that's how we'd then drive the car so we've got the three functions that were in green on the presentation at the start so these are the three functions now that will copy to every car that implements this class so they won't have to redo these functions these on that work so that's them out the way then in order for the certain things now that we're going to say we simply don't know we can't make a presumption that all cars act a certain way or have a certain thing so one of those things is we're going to say the wheel size we we can't make a presumption because every cars got different wheels but so we're just literally going to leave that as an unknown but we do know a car has wheels so this is where we're talking about an abstract class is that it's a known thing so we know that the handbrake is released or it's not released and we know we can turn the radio on and off and we can drive and we know a car has wheels both unlike being able to turn a radio on or off by default or release a handbrake by default we're going to say that the wheels we simply don't know but we know for a fact it has to exist so this is where you define an abstract property in this case so this is going to be a property that we know what all cars need but we don't know anything about it we don't know the value we don't implement it so you define it just like a normal variable or a normal property in this case and we'll call it wheel diameter and it's going to be an inches and it's going to be a gutter only so the cars wheel diameter now that's a normal property now if we do instead abstract here this means now that this any class that implements car is now going to have to override not override but actually create this integer so it's forcing a class to implement a wheel diameter so we then make model and go ahead and start making a public class budget car and you'll see now at the same budget car does not implement inherit abstract methods member wheel diameter inches so you can click on there you can click control and dot and then you can press ENTER to implement abstract class and you can see it's forcing us to implement the wheel diameter inches so in this case we'll return at say 16 so this is a way that if we now make an instance of a car we you know it we create a subclass of car then we're going to be forced to implement the wheel size and that's kind of and then one of the points of an abstract class is to sort of force anything that wants to implement it they can be forced to implement specific things so it's we implement in the base class you know in this base abstract class things we know about that we want functions off and these can be anything but then the might just be a few small things that we don't know about so instead of having to copy the logic all the time and redo a load of logic in every class so you can simply define something that's known but we don't have the value for and then you can make use of that inside of functions without having to you know have the explicit value so it'll come clear it as we you know finish more things often here so we're going to have add another few things so we will have in order to calculate the the speed originally in the slide we had it so that each car was going to calculate its own speed but really we can calculate the speed of the car by simply having the wheel diameter given to us and everything else is the same so unlike the slide where we had calculate speed is an abstract we've changed up the wheel diameter has been an abstract and we're now going to implement a calculate speed function that's going to return a float I'll call this calculate speed in miles per hour and this one isn't virtual because we don't want the calling plaster you know this budget car and sports got to be able to replace this function because this is a known mathematical function so we don't make it virtual so therefore it can't be over in so it's just a regular class function like any other so to calculate the speed well one if the handbrake isn't released then the speed is going to be zero because we're not moving and that's the presumption was made because the drive won't drive until the handbrake is released so if there's no handbrake or hand brakes on we are not moving otherwise we then want to first get an order to calculate miles per hour we've got the wheel which is in inches so this is nothing to do with abstract classes we listed some mathematical equations so I'll kind of skip over it fairly fast what we want to first get inches per revolution so basically how far distance wise along the road we will travel for every rotation of the wheel so it's a fairly simple math equation for anyone that knows any kind of math and that is just the diameter of a circle which is pi times the diameter so this will convert say a 16-inch wheel into like a 48 inch flat surface roughly so you'll travel 48 inches every time they're 16-inch wheels done one full revolution that's the first part of the calculation now you want to get the integral travel per minute so if not inches per revolution so now you want to know how far you'll travel in a minute so in order to do that you get the inches per Rev you know inter times and you need to know at this case what the gearbox is outputting in order to because the gearbox spins the wheel so the engine spins which then goes through the gearbox and then to a ratio it comes out and moves the wheels after it's gone through a differential book the basics are we need to know the output speed the revs per minute of the gearbox so let's just also make a quick function here called public float gearbox out for Speed Graphic a minute and again don't worry this is not really specific to abstract classes but we'll fill it in so we get some functionality so just can say my gearbox is spinning the wheels a thousand revs a minute so then if we turns up by gearbox output speed will now know how many inches are minute the cars traveling based on the wheel size and the gearbox which is now going to stay fixed this cars going to drive with the engine and the gearbox spinning at the exact same revs all the time just for this demo so the final phase there is to simply convert it to miles per hour and to do that you just want inches per minute into inches per hour so times 60 so they got inches per hour now you just want to convert inches tomatoes which is by tans knit by a small number of I think it's one two three four zeros in five seven eight to eight kind of sad that I know that number as well and I think I was right so that should convert inches per hour into miles per hour so once we've got that we return the miles per hour and that's how a function we can call so we've now got some functions we've implemented we've got the radio arm they've released handbrake the drive they can all be overridden with new functions then we've got standard methods that are based of the gearbox speed and the speed the car traveling so that for them in there and then the other two things we said in the slide the abstract parts of the car the things we don't know anything about one is this wheel diameter which is a property and the other two are going to be functions they're going methods and that's 1/2 wind the window down and to to move the seat forward so again just like create a normal method which would say be public void lined window down that's how you create a normal method to make it abstract like everything else just put the word abstract in front and now you should see that you don't want to implement an abstract so because you don't know anything about it you don't implement the method is employed semicolon so now this is an unknown method we can certainly call the method inside of this class but the actual implementation of what that does is not known and it doesn't need to be so it can't because you can't create an instance of an abstract class it doesn't matter that this could call it in here so you could say in here why I'm doing the damage but this class has no idea what winder and down does but it doesn't stop it effectively calling it at this point in time because whenever radio on is called it can't be cause and call it has to be called from say budget car so therefore you can add a logic into your abstract class but make use of unknown information so again that's all I'll try and point out some good real-world examples after this F of Y truthful so this will lines the windows down and it's not implemented yet and then we'll also have the abstract void move sit forward it's falling forward room there we go so these two now you'll see here need to be implemented so click budget car control dot and enter you can see now it's implemented these three things so now any car that we create again as the diagram shows we now need to implement these three things for it to become a full car that we can make yourself so we don't make any presumptions of how a seat moves forward in any car so we then have to implement it Kerr car so in here again we'll just do console dot write line moved seat forward using hand lever and then the wine window down console dot right on line round window down slowly using a wide definitely called the manual lever so this is a budget car implemented so now in fear we can do for budget car equals new budget car we can create a budget car so now we have an instance of budget car and then we want to make use of that so we will have say or the console dot write line project car and then we'll put some like underlined type of head of there and then we'll create the instance of the budget car and other than plus eight do stuff a car will pass in budget car so that we can you know do this exact same thing for the sports car afterwards and then in this function do stop the budget car we're not going to pass in budget car when I pass in the abstract class because that's all we need to know the abstract class contains all the the known you know that it contains the methods and we can now call them it's just that you can't create an instance of car so by passing in an abstract car we can make use of this function for any kind of car and still call the method there specific to like budget car so in here we're just going to say and then I move the seat forward put the radio on line the windows down and drive and then we will log out the speed the car should be driving up so we will say driving act whatever miles per hour with something inch rims so then in here we will be driving a car dot calculate speed in miles per hour and car dot wheel diameter inches so basically I'll put some information about the car once we've done that we'll simply wait for the console doesn't close and if we run this now we should see that we've moved the seat forward the radios on with round the windows down and we drive so you can see move C forward using handle either turned on radio one window down slowly using winder successfully release the handbrake press the gas pedal and we're driving at forty seven point five nine miles an hour with sixteen inch rims so there's one card on and also you have to do to get all the methods of calculating speed and you know tuning radio is on we haven't had to redo that in the budget car we'll just have to specify the specifics the things that are specific to the budget car that are different per car so now we've got that one we can make them public class sports car click control dot and enter wheel diameter this one's going to have big 22 inch rims move seat forward we will have moose it forward using button nice and easy when window down fast with electric motor that's all that's different that's all about to do now to make a sports car so in here we can copy the budget car output and do a sports car output and then this will be sports car equals new sports car and this car will do stuff to the sports car and then I will see the output of that we should see all the standard stuff like turn the radio on is the same and it's come from effectively the base car and we have moves afford using button we're driving at 65 miles an hour because the wheels are bigger but in essence all the other stuff is the same so we've managed to get and we'll also do a custom new line so that we have some space between there so we've got two different cars that were just up to implement you know the specific functions and then they've gained all of the properties like the handbrake release the methods and everything else from the base class and then they just have to implement specifics that offer the abstract class so the one other example here will say we'll make a broken budget car so we'll do a public class and write the title I'll just copy the budget car once more paste it and we'll call this one broken budget car and we got all the same stuff which is fine so that's that's okay we don't need to do anything about that or rather let's just simply and have it a budget car we don't need to do that at all so we've got a budget car we're going to make another version of it that's now broken so for that we're going to override and this is why we made the other functions virtual we're going to say in this case the release handbrake we're going to make this our own function in this broken version because we made the function virtual here which means we've got an implementation so we don't need to implement it but there's an option that we can you know it's not like an abstract where we have to implement you know you've got to make an instance of this because nothing exists a virtual map that exists but you can simply make a different version of your light by overriding so in the budget broken car we're going to override the handbrake I'm simply going to say a handbrake religious force we're never going to release the handbrake so handbrake is always on then we're going to do console.writeline exclamation for this an error and very failed to release and then we're going to see what impact that has now so in the budget car when it changes to a broken budget car and we're going to run that thing as you now we've moved it forward returned the ready one round the window down and says he a handbrake failed to release and now cannot move handbrake jammed driving at 60 miles an hour grabbing at naught miles now with 16 inch rims so effectively we have implemented a budget car that now doesn't release the handbrake but when we sell it to drive which we haven't told it how to drive you it's pick that up from the abstract class its figured out that you know it can't move because the Hambridge jump that's free light line so that's really the the abstract class here as car that implements things that are the same for every car like you know calculating speed but then it needs to know specific information about the master classes or the classes that implement the object class so that's all good and true that's how it's physically done and in essence you just stick the keyword abstract before what you want to be abstract and that makes it abstract so I guess one of the questions might be you know why would use an abstract class say instead of an interface why not just make all cars have an interface with wheel diameter handbrake released radio on all this that the other the key thing with an interface is you can't implement anything in an interface so if you make an interface then you can't make any instance you know you can't define these functions and interfaces is not a definition so you wouldn't use an interface because we want to explicitly define these you know these methods the known pieces of information that we want to implement so that's why you wouldn't use an interface so the next question might be well why wouldn't you just make a you know base class why'd you need abstract all of you delete the word abstract now you've got to then define you can do abstract inside because it must be contained in an abstract class so you'd have to remove abstract from here which now means you have to define wheel diameter which by default is zero so that's not going to break anything and then you'll come down and these functions of them abstract again so you definitely abstract you'd have to make versions of those methods and now you could create an instance of a wheel car a wheel car you could create an instance of car which is you know no longer abstract and you could attempt to you know do things with it where there's the wheel diameter zero so it's not really valid the car can have zero diameter wheels and also when you come to say making new version of the car so vided public new car implements car or public plus at help you can see now I get no warning I get no prompt to even do anything so now the the person implementing a car might say it's all cool I've now got a car I'm going to go and figure out well what's on the car let's have a look this again all the class properties are getting a load of stuff it's like well what do I even do what's you know the mat start typing and try and find out so I probably could override and see what they can do or they might have to then create a constructor and do follow look there's wheel diameter so they're going to have to figure out you know how to do things it's not the same so you're giving no indication to the implementing class of anything that's expected you know you're not forcing a behavior onto being implemented all right you can also then just go and happily create a new instance as we mentioned of car which is not really valid because it's a it's a partial definition it's got partial unknowns in it and so the reason why you wouldn't use a base class is basically because this functions that you want to force onto the implementing class so you're expecting them you know you fought in these things that you need to know explicitly on the class so it simply would not work without that in there so we just run back in time to and the class's abstract you know forcing these answer if we didn't implement a wheel size and we were trying to make a new instance of the car it literally will not compile it's a very forceful way to make sure anything implement in a car it's going to implement what you need it to and you're expected things and the second reason for an abstract classes also to make sure you can't create an instance of car because it's again it's partially unknown so it's not that you can't use a base class you can do everything with a base class that you can with an abstract it's simply that an abstract is a more appropriate way of defining what you expect this class to be you know on what information you need from it and that's how you create an abstract class then how you'd implement the abstract classes and how things are different on them with some output and I'll just go ahead and open a few things that are again real-world examples where we are actually using them in some code so go ahead and open those now and we'll take a look so the only real example I could find because we don't use them that often is this base of that property in the cassette or word application we're building and here I make an abstract class for base attached property we don't do any abstract properties or methods because there's nothing we explicitly need from a class that subclasses this it's it's more that we add all of this logic into the class we define you know a value itself the attached property we do all of the complicated stuff and the key thing here then in the abstract classes that we simply make some virtual functions or in this case oh yeah virtual functions and it's when the value changes I'm going to value updates and by default we make them in nothing so the nothing happens when the value changes which is perfectly valid but you typically expect to override one of these two things so we could make them both abstract and force both of them to be implemented but then you'd often have one of these blank so in this instance we make two virtual methods in the abstract class and that's all that has to be done by then classes that implement this so or the inherit all of this code and they can simply now implement the base attached property so an example of where this is then use we scroll up and look at references we have as a bad way of doing it let's do where are we here so say auto scroll to bottom property and implement the base attached property and we passing this also a generic so we pass in ourselves and then we override like we saw in the example this on value change so when the value changes we do something specific and that's all we care about so now in order to create an attached properties we have to do is override this abstract base class and that whole thing will link up and work and whenever the value of that attached property changes we override the method to them do something specific so it allows us to make very small and clean attach properties by using this abstract base class and again if we didn't make it abstract that would mean that somebody could try and you know create a base attach a property which you know wouldn't really work because it's it's not designed that way so this is an actual working example of what we use and the reason we've chosen this over say again the two definitions are why would you choose an abstract class instead of an interface or a base class why you wouldn't choose an interface because you couldn't define all of this data and you wouldn't use a base class because we do not want the base attached a property to be instant' a table of you will you don't want to instantiate a new class because this isn't by definition complete it's not going to do anything it's just generic so you know you attach this what's it expected to do nothing's been defined there's no behavior in the on value change draw dated which is the point of it so you don't want to instant ate it and those functions that we want implemented which we're not forcing with abstract here because the user can choose between one of two or they can simply do things on both but we could have quite easily made these two abstract but again there's no need we've made them virtual so they can be overridden so an abstract class doesn't need to have abstract content inside of it by doing a you know class like this the main benefit is they simply cannot create a new instance of base attach property and again that effectively defines the expected usage of this class you're telling the developer that every try and creating a new instance that's that's not the right way you're doing things you you haven't made this class for that reason you've made this class purely as a base class and you're forcing that by putting abstract there and then on base classes that are abstract that the developer then knows to look for you know overridden methods so if I was going to then implement an abstract class like this one and violin did probably Class A and it was a beta packed property of a and a boolean and I know it's an abstract class angle n just simply tough a keyword override and see what I've got and these are object because every class as an object so that vitam two strings are all built in and I can quite clearly see now these are two things I can implement on that base class so it's kind of again what developers are expected to do have you implement an abstract class the first thing you'll do is you might be forced to implement in things and then you know about them and then the second data type the word override and have a look at that what makes you know what can I do with this and these are the two variants that you can do this is the only two things you can really change about the class so therefore that's that's kind of the the intended purpose of this abstract class and it's been given via either forcefully having to implement abstract things or in the override keyword so that insurer really is is what abstract classes are and what they used for again you don't use them more impact this was the only real example I could even find and where I've used them recently but they do have a purpose again they can they can certainly be done you can do everything without abstract it's not a requirement it's simply more of a defined intent to you know force the correct usage of a class that you're making so hopefully that's been I guess the best way I can explain an abstract class because they are you know fairly specific and the simple but also they've got the the key purposes and like one is that they shouldn't be created on their own they're not designed to be used as is and the second is to force any class to implement the base class to correctly set up instantiate and you know work correctly with that class so again any questions and if you don't clear just simply leave a comment but hopefully this was useful and yeah that's it you [Music]
Info
Channel: AngelSix
Views: 10,911
Rating: undefined out of 5
Keywords: c#, beginner, tutorial, abstract, class, classes, why, what, guide, programming, object, oriented
Id: mq7Jul_HrZM
Channel Id: undefined
Length: 37min 35sec (2255 seconds)
Published: Wed Aug 23 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.