Search This Blog

Thursday, June 14, 2018

Sealed Method


Sealed keyword is basically used to prevent from inheritance. If class is declared as Sealed that means that class cannot be inherited. Similarly, if a method is declared as Sealed that means that method cannot be override in the further inherited class method. Sealed in method is always used with override keyword.

Sealed method is used to define the overriding level of a virtual method. By declaring method as sealed, we can avoid further redefining and overriding of this method. In other word's we use "sealed" on a method if we didn't want any derived classes to further override out method. A sealed, overridden method prevents additional overriding.

The sealed modifier in the method always used with override. Because the intention behind sealing a method is to stop further overriding of it.

In other words, at any level of inheritance, if you want to restrict the next level of derived classes from overriding a virtual method, then create that method by using the keyword sealed along with the keyword override.

Abstract modifier cannot be use with sealed class, because an abstract class must be inherited by a class that provides an implementation of the abstract methods or properties. When applied to a method or property, the sealed modifier must always be used with override.

1.       Because structs are implicitly sealed, they cannot be inherited.

2.       It is not necessary that a sealed class should also contain sealed method. As, sealed class itself means that no class can be derived or extended from it, so, there is no question that a method can be overridden and implemented by derived class. Hence, there is no sense of having sealed method it.

3.       Any method can be sealed in the normal class which allow extension, so no derived class can override its own implementation. In very simple word, if I don’t allow a derived class to override it accidentally and write his own definition, I will make it sealed. “Accidentally”, I said because if you can remove the sealed keyword in base class method and override it in your derived class but you will think of the intention of making it sealed and act accordingly in the program. By the way this is the best practice.

4.       Sealed class and sealed method in C# programming is used to stop a developer to extend a class or override a method accidentally. If he does so, C# compiler will flash an error. If he really wants to extend or override he can remove the sealed keyword in C# program.

If we try to override sealed method then it will throw compile time error that “cannot override inherited member because it is sealed



E.G.
class ChildClass : ParentClass
    {
        public void Test()
        {
            PublicMethod();
            ProtectedMethod();
            SealedMethod();
        }
        public sealed override void SealedMethod()
        {
            base.SealedMethod();
        }
    }

    class Program: ChildClass
    {
        static void Main(string[] args)
        {
            ChildClass childObject = new ChildClass();
            childObject.SealedMethod();
        }
        //public override void SealedMethod() //cannot override inherited member because it is sealed
        //{
        //}
    }

Sealed Method Vs Private Method

No comments:

Post a Comment


This is a User Friendly Blog.
Simple Interface and Simple Controls are used.
Post your comments so i can modify blog regarding your wish.