Sealed Classes क्या हैं
एक sealed class ऐसी class है जो lock की गई है ताकि कोई इस पर build न कर सके, जैसे एक पूरी हो चुकी sculpture जिसे बदला नहीं जा सकता। कोई इससे inherit नहीं कर सकता।
In this page:
Syntax
sealed class ClassName
{
// cannot be inherited
}
// or seal one method:
// public sealed override void MethodName() { }
Sealed Classes
एक sealed class से बिल्कुल भी inherit नहीं किया जा सकता, किसी भी आगे की subclassing को रोकते हुए। आप एक individual override method को भी seal कर सकते हैं ताकि गहरी subclasses इसे फिर से override न कर सकें।
Sealing अक्सर security, performance, या किसी ऐसे design को lock करने के लिए उपयोग होता है जिसे extend नहीं होना चाहिए।
Note:
किसी class को seal करना compiler को कुछ optimizations apply करने दे सकता है क्योंकि type hierarchy वहां रुकना जाना जाता है।
उदाहरण: 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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- किसी
sealedclass से inherit करने की कोशिश करना, जो एक compile error है। - किसी ऐसी class पर
sealedउपयोग करना जिसे बाद में extend करना हो। - किसी method को बिना
overrideकेsealedचिह्नित करना, जिसकी अनुमति नहीं है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: