All right.
So in this video we're going to look at the sealed keyword.
The sealed keyword allows you to make sure that a class cannot be inherited from.
And therefore I'm in this example where we had person employee person and manager.
Okay.
So to really make it simple I'm just going to show you how to use it.
And then I'm going to show you a really good example which will help you understand why you would use
it.
Okay.
So you use the keyword just before the class keyword.
So you use sealed here.
This way you seal the class from inheritance.
Now if you go down you will see that manager cannot inherit from employee.
It says manager cannot derive from sealed type employee.
So employee is now sealed.
Therefore any other class cannot inherit from employee.
And that is really it.
That's what the sealed keyword does.
Now why would you use it?
That's the better question in my opinion.
And let's look at an example.
I'm going to prepare some code and this will make it a lot easier to understand.
Okay.
So now I have a class called account which has an account number a balance, a constructor you can deposit
and you can withdraw okay.
And then we have a savings account subclass.
But this one is sealed.
So in the savings account we are defining some additional details about how the withdrawal works.
And that's pretty much it.
Right.
So that is a sealed class.
Why would we now seal the savings account and why would we use the sealed keyword in general.
So the reasons to use the sealed keyword is number one design integrity.
So sealing a class ensures that its design is final and cannot be altered through inheritance.
This is crucial when you want to enforce specific behavior and prevent modifications that could compromise
the design.
An example in our banking application is the savings account class that has specific withdrawal rules
that should not be overridden by any further derived class.
Number two is security, so preventing further inheritance can enhance security by avoiding unintended
behavior or misuse.
By sealing a class, you ensure that critical functionality remains consistent and secure.
The example.
By sealing the savings account class, you ensure that no unauthorized or accidental changes can be
made to how withdrawals are processed, maintaining the security of the account operations.
Then there is performance.
Sealed classes can sometimes lead to performance optimizations.
The runtime can make certain assumptions about sealed classes, such as method calls not needing to
check for overrides in derived classes, which can result in faster execution.
So in high performance applications, sealing classes that are not intended to be extended can provide
slight performance improvements by reducing the overhead associated with virtual method calls and then
preventing misuse.
So sealing a class prevents it from being used as a base class, which can be important if the class
was not designed with extensibility in mind.
This avoids potential misuse or errors from improperly extending the class.
So if the savings account, for example, is not designed to be extended with additional account types
or behaviors, sealing it ensures that developers do not mistakenly try to extend it inappropriately.
So these are some good enough reasons, I believe, to see why the seal world would be useful.
You might see the sealed word every now and then when developing in general.
So this is just good for you to know.
All right.
So that's it for this video.
I'll see you in the next one.