C# sealed class example

Let's learn sealed class in c# programming or how to use sealed keyword while creating any class or property in c# program.

Sealed is the keyword used for protecting any class or properties from being derived from other class, let’s see some sealed class example.

sealed class SealedExample
{
    public int getTotal(int x, int y)
    {
        return x + y;
    }  
}

Now to access the above method we can create a new instance of the class, but cannot inherit the class.

In the class below, we have created a new instance of the class and accessing the method, the code will work fine.

public class test1
{
    void show()
    {
        SealedExample se = new SealedExample();
        int result = se.getTotal(2, 5);
            
    }
}

Now instead of creating instance if we try to inherit the class and access the method, that will cause and error, will not work.

//this will not work
public class test2 : SealedExample
{
    void show()
    {            
        int result = this.getTotal(2, 5);
    }
}

Because the class sealed class SealedExample is being protected with "sealed" keyword.

Sealed property in c# programming

Now instead of protecting the class we can protect any property or method using sealed keyword.

We cannot directly use sealed keyword for property or method for declaring in class.

//this is not allowed, will give compilation error. 
public class Car
{
    public sealed int getTotal(int x, int y)
    {
        return x + y;
    }  
}

We can have sealed property or method in a class only with override keyword, means sealed key word can be used for methods only when we want to override the base methods and want to protect from derived class.

In below code, the see the example of sealed protected override sealed protected override string getModel(int modelno)

public class Car
    {
        protected virtual string getModel(int modelno)
        {
            return "model details";
        }
        protected virtual string getEngineDetails(int modelno)
        {
            return "engine details";
        }
    }


public class Maruti : Car
    {
       sealed protected override string getModel(int modelno)
        {
            return base.getModel(modelno);
        }
        protected override string getEngineDetails(int modelno)
        {
            return base.getEngineDetails(modelno);
        }
    }
    

public class TestCar : Maruti
    {
        protected override string getEngineDetails(int modelno)
        {
            return base.getEngineDetails(modelno);
        }


    }

The method sealed protected override string getModel(int modelno) cannot be accessed in "TestCar", which is inherited from “Maruti” class.

Sealed methods or property can be used in system designing while you have complicated situation where you can allow some methods to be accessed in some certain level only.

You may be interested in following tutorials:

 
C# sealed keyword example
.Net C# Programming Tutorials
Online Tutorials
C# .net Interview Questions Answers
.Net C# Examples | Join .Net C# Course