What is a sealed class in C#?


Sealed class in C# with the sealed keyword cannot be inherited. In the same way, the sealed keyword can be added to the method.

When you use sealed modifiers in C# on a method, then the method loses its capabilities of overriding. The sealed method should be part of a derived class and the method must be an overridden method.

Let us see an example of sealed class in C# −

Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         Result ob = new Result();
         string str = ob.Display();

         Console.WriteLine(str);
         Console.ReadLine();
      }
   }

   public sealed class Result {
      public string Display() {
         return "Passed";
      }
   }
}

To access the members of the sealed class, we need to create the object. The method created inside the sealed class cannot be inherited −

public sealed class Result {
   public string Display() {
      return "Passed";
   }
}

Updated on: 21-Jun-2020

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements