Wednesday, July 19, 2017

Sealed class in c#

This article illustrates about a  sealed class. When a class use sealed modifiers, it prevents other class inheriting from it. If you derive a class from sealed class it will be an error.


Example:

class student
{
    static void Main(string[] args)
    {
        SeaClass seaCls = new SeaClass();
        int total = seaCls.Addition(7, 9);
        Console.WriteLine("Total = " + total.ToString());
    }
}
// Sealed class
sealed class SeaClass
{
    public int Addition(int x, int y)
    {
        return x + y;
    }
}  

If you run this code the code work fine but if you try to derive a  class from sealed class it will be an error.

No comments:

Post a Comment