← Back to C# Course | Chapter 7: Inheritance | Lesson 5 of 6

Sealed Classes क्या हैं

एक sealed class ऐसी class है जो lock की गई है ताकि कोई इस पर build न कर सके, जैसे एक पूरी हो चुकी sculpture जिसे बदला नहीं जा सकता। कोई इससे inherit नहीं कर सकता।

In this page:

  1. Sealed Classes
Syntax
csharp
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

markup
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
    }
}
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. #}
आम गलतियां
  1. किसी sealed class से inherit करने की कोशिश करना, जो एक compile error है।
  2. किसी ऐसी class पर sealed उपयोग करना जिसे बाद में extend करना हो।
  3. किसी method को बिना override के sealed चिह्नित करना, जिसकी अनुमति नहीं है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.