Abstract Class in C# and WHY you need to LEARN this

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube this is dennis paneuta for tutorials.u in this video we are going to look at the abstract keyword and this is part of a series that i am starting and it's about going through the keywords that we have for c sharp so there are a bunch of c sharp keywords out there and i want to look at them one by one so you can see here i want to go through them alphabetically and we're going to look at the contextual keywords as well you see there are a couple more that are going to be very interesting so we're going to start with abstract in this video then at baseball and so forth so you see there are a bunch of keywords we need to look at and the alphabet is our friend so we're going to start with the letter a [Music] okay so let's get started let's look at a project that i have here so i have this project with well my default class program cs and by the way it just has console.redline in it so this is the new console template if you are wondering where this comes from i have a video about it online as well check that one out it's going to be in the card and then you will basically understand what this all means so as you see there is no boilerplate code anymore with the entire namespace using keywords as well as class keywords and main method and so forth it's all directly usable here so this is as if we were in the main method so here console red line is going to run something on the well in the main method so to speak so what i have is an animal class and i want to look at the abstract keyword so you see we have this class called animal and i'm expecting for you to know what classes are obviously otherwise this doesn't make sense so we have this class called animal and we want to have a class called dog and then later a class that is called cat as well in order to use the abstract keyword what you can do is you can make for example a class abstract so here this is going to be the class animal which will be an abstract internal class internal is an access modifier which we're going to look at once we're at the ledger i as well so now inside of this abstract class i can go ahead and create abstract methods and the abstract keyword can also be used with properties indexers and events so classes methods indexers events and properties okay and then let's go ahead and use it for a method so here i'm going to create a public abstract void make sound method so this method you see doesn't have a body because this is an abstract method the abstract keyword or modifier indicates that the thing being modified has a missing or incomplete implementation so in this particular case we can see that this class animal has something missing and in particular we can see for this method make sound so this makes sound method is incomplete there is no body so it doesn't have a method body of what should be executed once this method is being called so that means that once we are inheriting from animal we can then what we have done to do the implementation so let's go ahead and look at this class dog so dog here is going to inherit from animal okay so now once you do that you can see the dog creates an error doc does not implement inherited abstract member animal makes sounds so we now need to go ahead and implement the method and well by implementing this method you see we are adding this public override void makes sound and now we need to add something here so we need to say what should happen once we want to make a sound as a dog so currently it's throwing a not implemented exception which means well we haven't wrote any code here yet so if we run the application right now obviously nothing would happen because we are not creating any dog objects but now let's go ahead and create a dog object here so let's say dog let's call him sif it's going to be a new dog and for dog to be available we need to either use abstract demo dog or we need to use using abstract demo so now this will allow us to access the classes inside of extract demo so now let's use this if and call its method make sound okay like so and now if i run this code we will get the not implemented exception so this error will be called and we can directly see okay well this is where we need to implement something so let's just do that so let's go ahead and get rid of this and use a control right line statement here and this one would be something like woof for example okay so now if we run the application again we should see wolf here all right so now what we have done is we've used the abstract keyword to create a class which uses an abstract method where we don't implement it at this point yet but whichever class is inheriting from us will need to go ahead and take care of the implementation so that's what the abstract keyword is really for so now go ahead and create a class called cat and implement the make sound method there as well by first of all inheriting the animal class obviously quick pause in this video you'll learn something about c sharp and if you want to learn everything there is to know that you need for the fundamentals and to become a real c sharp developer then definitely check out my c master class in which you're going to learn all of the things you need to know about c-sharp so you're going to learn how to do the basics how to use object-oriented programming how to use wpf in order to create your own user interfaces how to use databases how to use link how to create your own games using unity and a lot more so if you want to become a real c sharp developer definitely check out the link in the description below okay i hope you tried it so here you can go ahead and add a new class or you can press ctrl k and c and then this window will pop up i'm going to call this one cat dot cs which will be a general class let's add that and now cat should inherit from animal like we did before and now obviously cat needs to implement the method that is available inside of our animal class so here show potential fixes and then we can just now we can just implement the missing methods you can see make sound has been added and the override keyword is being used as well because we are overriding whatever was defined in the parent classes method so we're going to look at the override keyword once we're at the o letter but generally speaking we're just saying whatever was defined inside of animal for what makes sound should do we're going to override that with our own code so to speak and that's what we're doing so here instead of throwing in not implemented exception i want to do something like meow okay so that's what the cat is gonna do and now let's go ahead and create a cat object so cat my cat or let's call it mole which was my first cat let's create this new cat and let's call muller.make sound to just check it out and we see meow is being called as well so that is what the abstract keyword can do for us now there are a couple of things that we need to consider so an abstract class cannot be instantiated so let's look at that so let's go ahead and try to create an animal object my animal or let's see animal new animal like so you see this doesn't work even though the ide auto-completed all of this code and created this new animal but that just doesn't work because cannot create an instance of the abstract type or interface animal and then whenever you are using the abstract keyword you cannot use the seals keyword so you cannot use the sealed modifier here you cannot say abstract sealed internal class animal because what those two keywords do is they basically do the opposite of each other so abstract requires a class to be inherited to do anything because animal by itself doesn't do anything right and sealed prevents a class from being inherited so we're going to look at sealed once we're at the s letter but now you have an idea of what the abstract keyword does for you the same concept applies as i said at the very beginning also for events properties and indexers all right that's it for this video i hope you enjoyed it if you did then please leave a like and also subscribe if you haven't done so already the channel is pretty small still we've just started with it and yeah looking forward to creating a bunch of really cool videos in the future for you [Music]
Info
Channel: tutorialsEU - C#
Views: 1,932
Rating: undefined out of 5
Keywords: Tutorials, Tutorial, Programming, Course, Learn, Step by step, guide, development, programmer, video course, video tutorial, learn how to, how to, Udemy, Denis Panjuta C# tutorial, C# tutorials, C# beginners, C# video, C# how to, Denis Panjuta C#, abstract class, visual studio, abstract class c#, c# abstract class, c# interview questions, abstract class vs interface in c#, c# interface, code, programming, programming with mosh, iamtimcorey, freecodecamp, net 6.0, net 6, c sharp
Id: dkFYUUWdQYI
Channel Id: undefined
Length: 9min 13sec (553 seconds)
Published: Wed Dec 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.