C# Abstract Classes - What They Are, How to Use Them, and Best Practices

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I get asked about abstract classes a lot what they are how they work and why you should know about them an easy way to think about an abstract class is to say that it fits between a full base class and an interface basically it's a blend of the two in this video I'm gonna demonstrate what an abstract class is how to use it and when it makes sense to create one now if you're new to this channel my name is Tim quarry and it's my goal to make learning c-sharp easier this channel is full of videos explaining the various parts of c-sharp now also a website where I provide full courses on c-sharp and sequel you can check it out at I am Tim quarry comm that's also where a blog post for this video is located it includes the source code from my sample application both the start of this video as well as the end also note that in the description below you can find links to a mailing list and to my patreon page okay let's get started in our example project for this application I really cut out all the code that would be a distraction I didn't want the setup to confuse the issue so I set a console application a class library and the class library have two data access classes well kind of they're actually just simulators so let's look at them the sequel data access class has a load connection string which just writes out hey I'm loading the connection string and returns just this test string the actual load data method just writes out I'm loading Microsoft sequel database data and I say this is I'm saving data to the Microsoft sequel server the reason I did this is because if I put code to actually talk to sequel you have to get that wired up and make sure you have a sequel server on your machine and all the rest in order to use this demo code and I think that really kind of hides what abstract classes do so I want to go that route the same thing the sequel Lite data access class it's just three methods which just do console.writeline so that's all that happens here now both of these if he knows are very similar connection string load data save data which would be an accurate portrayal of how yes simplified on how data access classes look so I create an interface called I data access the ID to access class has three methods in it load connection string load data and save data now just to demonstrate these working not but really need to run this application but in the console I create a new list with a new sequel data access and a new sequel Lite data access instance and then I do a for each and run through the three methods so if we were to actually run this we'll get those three methods called for each so sequel data access and then sequel Lite data access so that's all there really is to it so the point of this application is not a fully working application the point is just to demonstrate what an abstract class is and how it differs from both a base class and an interface so if we were going to create a base class if we say you know what I really like the idea of the fact that we can redo this code load connection string is the same for sequel did access as it is for sequel like get access which would be true in a real application probably so we create a base class so let's do that and we'll call this base class data access and I get public and we could do is we could take this method right here and I could put it in the base class and then instead of the implementing the ID to access which by way let's comment out our sample code here field it's gonna throw some errors otherwise so instead of in implementing sorry implementing the interface I did access we could just we could inherit from data access the base class and what that allows us to do is takeaway that's one method so instead of having to create load connection string each time it gets created in our base class data access which is cool we're now saving some coding because both of these sequel likely to access and sequel did access they're both children of data access and so they bring along in this case this one method so if we over here in our program dot C s if we were going to instantiate one of these just to demonstrate let's pick the sequel sequel like get access let's call it da equals new sequel likely to access notice that de a dot we have load and save data but also load connection string so great we now have this being inherited down the stack to our children sequel Lite and sequel data access so this demonstrates a base class which is not whether here in this video but I wanted to kind of start off with these basics we don't want to show you the differences so with its base class we have some implementation code right here that we're allowing they passed down to sequel Lite and secretly to access but what if instead of doing this we said data access guess what that works but that makes no sense why have a data access class we can instantiate that has a little bit of code but not all the code we need also what is it accessing is it accessing you know sequel sequel light no it's not actually any of those these doesn't have a load or save data and methods so this is kind of problem it's it's messy code and you can say well yeah you can leave it there and it's no big deal but that's not really what you want to do on the other side of things we have this I interface I did access which allow us to define load connection string load data and save data which allowed us here in this code down here to instantiate both sequel data access and sequel likely to access and treat them the same as far as knowing they both have loaded connection string load data and save data that's the other side an interface where abstract classes come in is where we blend the two together so let's change this data access to be an abstract class now that changing anything else what I've now done is I have allowed sequel Lite data access and sequel day to access to still operate as they were the base class but if I come over here and say data access DB or da equals new and goes you can't do that and if I were to try and force it it says I'm sorry you cannot create an instance of the abstract class or interface data access so it's not going to allow us to instantiate data access but if I were to say sequel Lite data access and I stole it right da dot we still have load connection string load data and save data which is great we now have an abstract class we've seen one of the benefits of an abstract class which is it doesn't allow us to instantiate it directly so a an abstract class says you can't create me directly but anything that inherits from me gets this code that's been at number one but we can go further than this remember I said that it is a it's a blend of interface and base class where is the interface portion of it well we have these two methods here public void load data and save data well I can come over here and declare public void load data and make one change public abstract avoid low dia nois a semicolon at the end similar to what we do in a interface and what this does is it says I'm not gonna actually have any code for load data because the data loads gonna look different for sequel versus sequel light but I know you have to have one therefore I'll declare here as an abstract now let's do it one more time this time for save data and then we'll look at how it affects our child classes so you have low data and saved it as both being abstract now over here we have a little bit an error it says does not implement inherit abstract member it looks kind of similar to what we have with an interface where it says does not implement the interface now load data hides inherited member what this really means is we have to say override okay so public override load data and save data now with it being an override what saying is I'm going to basically implement load data over here in load data and the way I do that is to override it so I'm overriding the declaration and actually creating the implementation of load data they have to do it over here as well so now we have that blend of both interface and base class we have some code that's coming from the base class that the abstract class and we have some code that's being declared from sorry some methods they're being declared but not actually implemented until you get to the child classes but say you have to end meant them the benefit here is if we come over here let's start over this and we say data-access da equals new notice it still says you can't implement data access itself but we could say sequel light data access and that works kind of like an interface now we can say da dot no to get the load data load connection story and save data we can do all three things because data access to Claire's all three things it's just that only implements the one it allows whatever class inherits it to implement the other two so that's really the the basics the foundation of an abstract class it blends both a base class and the interface so now we can rework our code down here so let's do that so uncomment this instead of ID to access we'll call it data access and that's it we now have a same exact code it's just instead using an interface we're using the abstract class as the class we call and now we can have both the benefits of those declarations here as well as the shared code now is one more thing I want to show you just in case it comes up in your mind or in case you need to do this and this is basically just class inheritance but I want to show you that it does still work in abstract class so what if you wanted to override this load connection string well if you came over here and let's just grab that definition here if you're going to try and override this and say oh let's add override that won't work and the reason why it won't work is because it's not something that's over writable if you want to override it you have to still use the keyword virtual that way you don't have to override it but if you want to you can't so here we can now actually implement this now it does say at the return of value which is fine so we could say string outputs equals base dots load connection string then with that base dot load connection string does it says okay the first thing I don't want to do is I want to call the data access load connection string and have it do its work you don't have to do that you can just do your own work instead but in this case I'm going to use both the base processing as well as some extra stuff so in here for the output I'm actually gonna say outputs plus equals from sequel Lite just to demonstrate that's actually working so I'm calling the base and getting its output and then adding from sequel Lite afterwards and returning that output so now they come over here and let's comment these three things out so don't have those distractions and we could say console.writeline DVD connection string demo doesn't matter we're actually using it demo and that's me for each the sequel data access as well as a sequel Lite data access if we run this notice we get a test connection string for the sequel data access but then test connection string from sequel Lite for the sequel Lite data access and that's when we run the load connection string method so we still have the ability if we allow it using the virtual keyword to overload or override the load in action string with their own version of it we can still call the bass version if we want and so we had that flexibility just leader to the base class it's just we also now have a flexibility of having these interface like member declarations okay that's all there is to base classes that's really it that they're pretty simple do you use those everyday no this is not something to use every day it's something that in certain specific cases you use now here is one big pitfall that I see people falling into you've learned about abstract classes and the same thing happens with class inheritance as well but you learn about abstract class and you go okay anytime I have two classes that have similar or the same code I can create an abstract base class and put that code there declare the rest and it works just as good if not better than an interface and that's not really true because you don't want to get into a messy inheritance situation inheritance really needs a limited to things that really are whatever their parent is declaring so parent in this case is a data access class well you shouldn't have something else that may need the load connection string but it's not data access you shouldn't have that inherit from data access it's it's the idea of you know car is a base class and then you have different types of cars that's great don't try and fit a truck in there too because the truck is not a car okay so try and use the is a relationship so just it's hard to say and get like that so you're trying to say you know X is a Y so sequel like data access is a data access class you know Dodge Neon is a car a toilet on Tundra is not a car okay so try and keep that is a relationship in place just like we do normally for object-oriented programming so keep that in mind just because this is a tool that's cool and it can bring some code in doesn't mean you shoehorn it into every different situation where you need shared code if you need shared code and there's not that relationship maybe what you do instead create a helper method or a helper class that both both pieces can access if you need to play around with different modifiers so instead of public you make it internal or protect it or something else in order to allow you to shape that to just what you need to give it access to but don't just say well I have shared code there for an abstract classes way to go okay so that's my word of warning I see it happen often enough that it's I think it's really important I stress it that don't just force an abstract class wherever you think it's needed instead make sure that that definitely follows the is a relationship first okay so I hope you've enjoyed I hope you found it valuable and if you have any questions if you have anything you want me to cover about this or about other topics leave that in the comments below I try and you know respond to every comic try and take every suggestion and at least put in a list and then as those suggestions come in I look at what's the most popular and try and tackle those this video is one of those videos that's definitely a direct relationship to that I had a number of people who are asking me about abstract classes enough that I figured it was important that we cover it in a video all right thanks for watching as always I am Tim quarry [Music] you [Music]
Info
Channel: IAmTimCorey
Views: 103,003
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# training, C# tutorial, c# interface, c# abstract, abstract class, c# training video, abstract classes in c#, interfaces and abstract classes in c#, interfaces and abstract classes, c# inheritance, c# inheritance and polymorphism, c# inheritance example
Id: jRkmPRk5j2E
Channel Id: undefined
Length: 19min 59sec (1199 seconds)
Published: Mon Aug 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.