<p>I have some classes layed out like this</p>

<pre><code>class A
{
    public virtual void Render()
    {
    }
}
class B : A
{
    public override void Render()
    {
        // Prepare the object for rendering
        SpecialRender();
        // Do some cleanup
    }

    protected virtual void SpecialRender()
    {
    }
}
class C : B
{
    protected override void SpecialRender()
    {
        // Do some cool stuff
    }
}
</code></pre>

<p>Is it possible to prevent the C class from overriding the Render method, without breaking the following code?</p>

<pre><code>A obj = new C();
obj.Render();       // calls B.Render -&gt; c.SpecialRender
</code></pre>

[{'answer_id': 43516, 'author': 'Matt Hamilton', 'author_id': 615, 'author_profile': 'https://Stackoverflow.com/users/615', 'pm_score': 6, 'selected': True, 'text': '<p>You can seal individual methods to prevent them from being overridable:</p>\n\n<pre><code>public sealed override void Render()\n{\n    // Prepare the object for rendering        \n    SpecialRender();\n    // Do some cleanup    \n}\n</code></pre>\n'}, {'answer_id': 43517, 'author': 'Matt Bishop', 'author_id': 4301, 'author_profile': 'https://Stackoverflow.com/users/4301', 'pm_score': 1, 'selected': False, 'text': '<p>In B, do </p>\n\n<pre><code>protected override sealed void Render() { ... }\n</code></pre>\n'}, {'answer_id': 43521, 'author': 'Scott Wisniewski', 'author_id': 1737192, 'author_profile': 'https://Stackoverflow.com/users/1737192', 'pm_score': -1, 'selected': False, 'text': '<p>yes. If you mark a method as Sealed then it can not be overriden in a derived class.</p>\n'}, {'answer_id': 43522, 'author': 'Ian Nelson', 'author_id': 2084, 'author_profile': 'https://Stackoverflow.com/users/2084', 'pm_score': 2, 'selected': False, 'text': "<p>Yes, you can use the sealed keyword in the B class's implementation of Render:</p>\n\n<pre><code>class B : A\n{\n    public sealed override void Render()\n    {\n        // Prepare the object for rendering\n        SpecialRender();\n        // Do some cleanup\n    }\n\n    protected virtual void SpecialRender()\n    {\n    }\n}\n</code></pre>\n"}, {'answer_id': 43531, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 1, 'selected': False, 'text': '<p>try <code>sealed</code></p>\n\n<pre><code>class B : A\n{\n  protected sealed override void SpecialRender()\n  {\n    // do stuff\n  }\n}\n\nclass C : B\n  protected override void SpecialRender()\n  {\n    // not valid\n  }\n}\n</code></pre>\n\n<p>Of course, I think C can get around it by being <code>new</code>.</p>\n'}, {'answer_id': 43547, 'author': 'bitbonk', 'author_id': 4227, 'author_profile': 'https://Stackoverflow.com/users/4227', 'pm_score': 0, 'selected': False, 'text': '<p>An other (better ?) way is probablby using the new keyword to prevent a particular virtual method from being overiden:</p>\n\n<pre><code>class A\n{\n    public virtual void Render()\n    {\n    }\n}\nclass B : A\n{\n    public override void Render()\n    {\n        // Prepare the object for rendering       \n        SpecialRender();\n        // Do some cleanup    \n    }\n    protected virtual void SpecialRender()\n    {\n    }\n}\nclass B2 : B\n{\n    public new void Render()\n    {\n    }\n}\nclass C : B2\n{\n    protected override void SpecialRender()\n    {\n    }\n    //public override void Render() // compiler error \n    //{\n    //}\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/43511', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3602/']