C#: Method Modifiers: Virtual/Override, Abstract, and Static

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the most popular modifiers for methods are the virtual override pair abstract and static in this lesson I'll show you how and when to use them so the first modifiers I want to talk about our virtual and override which always occur together a virtual method is in the base class and will always be used unless it is overridden by a method in a subclass this gives you a very powerful cascading effect of functionality let me show you how this works let's say we have a person class and a customer class which inherits from that and a employee class which inherits from person class as well so let's make the person class first public class person the employee class public class employee and an employee is a type of person and we also have a temir which is a type of person Jason has a first name as well as a last name so these are available in employing customer - because they're subclasses and these are public so if we say employee employee employee equals new employee we can define the first name and lasting name equals Jim and last name Smith now let's say we want to get a nametag from the person employee or from the employee or the customer we can put in person a method which returns a nametag so public string get name tag and on the name tag will be first name plus last name last name and so if we write this out employee get name tag and we run it then it does indeed say first-name lastname now let's say we decide though that for employees we don't want to put the first name in the last name but just the first name since it's a little more informal then what we do is we say this is a virtual method and by defining it as virtual it just means that it'll be used unless it is overridden by one of the subclasses so it'll still work just the way it did because it's not being overridden but if we go into the employee class and we say public over ride and Visual Studio will help us here get nametag and it gives us the base of what we need to create here as the method and we say here return just the first name and we run it indeed it only returns the first name because it is an employee if we had a customer let me just copy in a customer here so we have the same thing customer equals new customer first name and get name tag what's going to be displayed for Joe Thompson's name tag because he's a customer it's going to go here and say I don't have a get name tag base class you do it and so it'll just be first name and last name we run it and indeed it says Joe Thompson now we could in the same way override this one public override and Visual Studio is our friend and we say return for instance mr. will assume that they're all men for now last name and so if it's a customer he's has on his nametag a more formal name so that's how virtual and override work in base classes and subclasses now note that we still can instantiate a person so if we instantiate a person like this person equals new person first name last name and then print nametag what's going to happen well it's an actual person here so it will execute the get name tag inside person which happens to be virtual and it does but let's say in your application scenario you really will never instantiate a person the person is just there to be the sub part to be the base class of the employee and the customer then you could make this abstract like that and because it's abstract it has no body it's just saying this method needs to be inherited in subclasses which it is here so let's go ahead and run this and we get an error and the error is that get nametag is abstract but it's contained in a non abstract class ah so that actually makes sense we need to make the class abstract as well because the person will never be instantiated and so now we have an abstract class an abstract method let's run it and now it says cannot create an instance of an abstract class or interface if we go there we see that this code fails because as we mentioned our application scenario calls for not instantiating a person but the rest of the code works you can have employees and customers and so that is the use of the abstract modifier so that was abstract and now we have one more which is static and what two static methods - they are quite different from the other methods virtual override and abstract and regular methods because they do not need an instantiated object to be called and a good use of this is factory type of method which creates an object and so in our case here we would have that on person and it would be called something like create so it's public static I'm going to return a person and it's going to be called create and I'm going to get a string which will be either employee or customer whatever they want to create and then ask me the first name and the last name now I just need to make a switch statement based on kind so case it is employee then employee employee equals new employee employee first name equals first name and employee last name equals last name and that's what we have to return because we're returning a person but an employee is a type of person so it's no problem here and because we're returning we don't need the break here and we just need to do the same thing with customer so here we have customer we're doing the same thing if they try to create something else we'll just return a null for now so now we can change the way we create these objects so for instance this employee here I can say employee employee equals and then person create and you see that this method exists on the class not on the object that was instantiated from the class but on the class itself so we say create and then what does it need ah it needs the type okay it's an employee and the name is Jim and the last name is Smith and that's all we need and the employee is created but it has a problem here it's saying that an explicit conversion exists cannot convert type person to employ ah because in create if we go there go to definition create is sending back a person but we're defining an employee an employee is a type of person we just have to tell it that so we should say person create so get a person as an employee and then the compilers happy so we don't need this anymore and this should work exactly the same way and it does so in this lesson you saw how the most common method modifiers work the virtual override pair abstract and static
Info
Channel: Edward Tanguay
Views: 709
Rating: undefined out of 5
Keywords:
Id: YAgXX20o2Qg
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Mon Mar 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.