Answer:
A class that is defined as final class can not be inherited further. All Methods of a final class are inherently final and must not be declared as final in the class definition.
In java there is keyword “final”,but In c# there is no keyword like “final” but the same thing is achieved by keyword “sealed”.
namespace OOPS_Concept
{
sealed class SealedClassDemo
{
public void test1()
{
Console.WriteLine("Method Test1");
}
}
class childclass : SealedClassDemo
{
}
}
Above code will generate an error saying that class cannot be inherited.
In C#, Methods cannot be “sealed” directly. Methods of only derived class can be made sealed with keyword sealed and override.
namespace OOPS_Concept
{
class SealedMethodDemo
{
public virtual void Method1()
{
Console.Write("Base class Method1");
}
}
class ChildClass : SealedMethodDemo
{
public sealed override void Method1()
{
Console.Write("Derived class Method1");
}
}
}
Asked In: Many Interviews |
Alert Moderator