HOW TO USE SEALED CLASSES IN THE C# PROGRAM?

Barbara Jones

Total Post:19

Points:95
Posted by  Barbara Jones
C# 
C#
 289  View(s)
Ratings:
Rate this:
  1. Barbara Jones

    Post:19

    Points:95
    How to use Sealed classes in the c# program?

    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;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!