Can you allow a class to be inherited, but prevent the method from being over-ridden?

 Posted by Raja on 4/27/2008 | Category: C# Interview questions | Views: 7174
Answer:

Yes. Just leave the class public and make the method sealed.


Source: http://blogs.crsw.com/mark/articles/252.aspx | Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Posted by: Gryptonite on: 2/4/2013 | Points: 10
Sealed Example:

class A
{
protected virtual void SomeMethod()
{
}
}

class B: A
{
sealed protected override void SomeMethod()
{
}
}

class C: B
{
protected override void SomeMethod() <-- this will cause a comile error since class B has sealed it from being overridden
{
}
}

Login to post response