Sealed methods,how to use sealed methods
Yes. .NET allows the sealed methods.Sealed methods are used to define the overriding level of the virtual methods.Sealed keyword is always used with the override keyword.
namespace sealedMethod_Example
{
class Program
{
public class Base
{
public virtual void Test()
{
Console.WriteLine("Base Method");
}
}
public class Derived : Base
{
// Now the Test() has been sealed and can't be overridden
public override sealed void Test()
{
Console.WriteLine("Derived Class Sealed method");
}
}
//public class ThirdClass : Derived
//{
// public override void Test()
// {
//Here we are trying to override the Test(),but it's not possible becasue we have been sealed Test() in Derived Class So it will give error.
// }
//}
static void Main(string[] args)
{
Derived d = new Derived();
d.Display();
}
}
}
Yes. .NET allows the sealed methods.Sealed methods are used to define the overriding level of the virtual methods.Sealed keyword is always used with the override keyword.
namespace sealedMethod_Example
{
class Program
{
public class Base
{
public virtual void Test()
{
Console.WriteLine("Base Method");
}
}
public class Derived : Base
{
// Now the Test() has been sealed and can't be overridden
public override sealed void Test()
{
Console.WriteLine("Derived Class Sealed method");
}
}
//public class ThirdClass : Derived
//{
// public override void Test()
// {
//Here we are trying to override the Test(),but it's not possible becasue we have been sealed Test() in Derived Class So it will give error.
// }
//}
static void Main(string[] args)
{
Derived d = new Derived();
d.Display();
}
}
}
No comments:
Post a Comment