Sealed Classes
In this page:
Sealed Classes
A sealed class cannot be inherited from at all, preventing any further subclassing. You can also seal an individual override method so deeper subclasses can't override it again. Sealing is often used for security, performance, or to lock down a design that shouldn't be extended.
Note: Sealing a class can let the compiler apply certain optimizations since the type hierarchy is known to stop there.
Example: Sealed Classes
using System;
sealed class FinalConfig
{
public string Version = "1.0";
public void Show()
{
Console.WriteLine($"Config version {Version}");
}
}
class Program
{
static void Main(string[] args)
{
FinalConfig config = new FinalConfig();
config.Show();
// class Extended : FinalConfig {} would be a compile error
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: