Check out a free preview of the full C# and .NET Basics course
The "Sealed vs. Virtual" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:
Spencer explains the difference between the sealed and virtual keywords. He demonstrates how to use the virtual keyword and shows an example of overriding a method in a derived class. He also mentions the use of the "base" keyword to refer to the base class and how to seal a method to prevent further overriding in certain derived classes.
Transcript from the "Sealed vs. Virtual" Lesson
[00:00:00]
>> Spencer Schneidenbach: Talk about this virtual keyword. What the heck is that? So virtual is a keyword that essentially says, I want to have a default implementation. I want to have a default way that this behavior works, but I also want derived classes to be able to modify it if they so choose.
[00:00:20]
So this is where the tooling comes in because it makes it super easy to do. I can start typing an override and it gives me a list of things that I can override. Now, I mentioned that all objects have equal and equals method, a get hash code and a two string.
[00:00:34]
Those are all things that you can override based out of the class. Because get employee details is marched ritual as well, we can override that. So if we hit Tab, which I just did, and that basically just kind of put in all of the classes, all of the behavior here that we wanted to find the method for us.
[00:00:55]
Talk about a couple of things. First things first, we got this base call. Base Is what's known as the base class. You can see if we hover over it, it actually tells us base employee. Now, it has nothing to do with the base being in the name, and I'll illustrate that by just doing that real quick.
[00:01:14]
You can see that base refers back to the employee class, because that's what we inherited from. But we can also call the get employee details method. Maybe we don't have our own implementation or maybe we just wanna add something to this implementation. So let's say that we wanted to add in to this get employee details class, we don't just want it to show the first and last name.
[00:01:36]
If we're an hourly employee, we also wanna show the rate. So we'll do that with a nice little template string. So we'll say rate, and then we will say hourly rate, boom. And now when we call, our good employee details from our derived class, our hourly employee class, what are you complaining about?
[00:02:03]
Done that run. You'll see that George Harold's rate additionally prints 1, 2, 3, 4, 5. So we're able to not only override the default behavior of this thing because of the virtual keyword, we're also able to change the behavior. We can change the behavior completely if we wanted to.
[00:02:20]
We could return something nonsensical. In this case, we're just kind of buttoning on top of the functionality. If we did not have this virtual keyword here, we would not be able to override it the compiler, we'll throw an error. So super important, inheritance is a really important kind of critical part of understanding how C sharp class is and objects kind of work together within the runtime and in the language.
[00:02:45]
You can also, if you want to, if your inheritance hierarchy gets complex enough, you can declare a method as virtual here and then seal it here. And sealed essentially means that no derived class from hourly employee can now override this behavior. That gets into a whole different discussion about inheritance hierarchies and how deep should the rabbit hole go?
[00:03:09]
We'll get to that here in a minute.
>> Student: Because the implication is that if you created a different class, like salaried employee that inherited from employee or base employee, you would be able to override get employee details in a different way.
>> Spencer Schneidenbach: Yes, but if you inherited from hourly employees, so like public class, really sad hourly employee, You would not be able to, Yeah, you're not able to override that behavior because it's no longer here.
[00:03:40]
You can try copying and pasting it in. All the compiler is going to do is just say it's sealed you can't override it. Fun fact, string is a class and it is sealed. I can only imagine the kind of behaviors that developers could get into if they could inherit from string.
[00:03:54]
Thank goodness they're not able to do that.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops