Home > DeveloperSection > Interviews > How to use Sealed classes in the c# program?

Posted on    September-14-2016 4:58 AM

 C# C# 
Ratings:
 1 Answer(s)
  67  View(s)
Rate this:

Barbara Jones

Total Post:19

Points:95
Posted on    September-14-2016 4:58 AM

If we use Sealed classes in the program, then we can't inherit it.

In C#, the sealed modifier is used to define a class as sealed.

Below are two examples where we can use sealed key word for the class :

1st example

Below program will run successfully even after using the sealed keyword.

using System;

sealed class Restclass
{
    public int Add(int x, int y)
    {
        return x + y;
    }
}
class program
{
    static void Main(string[] args)
    {
        Restclass implecls = new Restclass();
        int total = implecls.Add(4, 5);
        Console.WriteLine("Total = {0} " ,total);
        Console.ReadLine();
    }
}

2nd example :

Below program will not run as Test class is sealed which cannot be inherit.

 

Public sealed class Test
{
public int Number;
public string Name;
}
Public sealed class child1:Test
{
public string Name;

Don't want to miss updates? Please click the below button!

Follow MindStick