- If we want to prevent a class to be inherited then we use the keyword sealed.
- When a class is providing full functionality then it can be declared as sealed class.
- The sealed modifier can be applied to classes, instance methods and properties.
- A sealed class cannot be inherited. A sealed method overrides a method in a base class, but itself cannot be overridden further in any derived class.
- When applied to a method or property, the sealed modifier must always be used with override.
- It is an error to use a sealed class as a base class or to use the abstract modifier with a sealed class.
- Structs are implicitly sealed; therefore, they cannot be inherited.
- Advantages of sealed class is it restrict the third party vendor for developing new software by inheriting from our logic.
using System; namespace sealedClassDemo { sealed class MySealedClass { public int Add(int x,int y) { return x+y; } }//_MySealedClass class Program { static void Main(string[] args) { MySealedClass sCls = new MySealedClass(); int total = sCls.Add(10, 20); Console.Write("Total is "+total); Console.ReadKey(); } } }
ANSWER: Yes, just leave the class public and make the method sealed.
0 comments :
Post a Comment