Open In App

C# | Sealed Class

Sealed classes are used to restrict the users from inheriting the class. A class can be sealed by using the sealed keyword. The keyword tells the compiler that the class is sealed, and therefore, cannot be extended. No class can be derived from a sealed class.

The following is the syntax of a sealed class : 



sealed class class_name
{
    // data members
    // methods
    .
    .
    .

}

A method can also be sealed, and in that case, the method cannot be overridden. However, a method can be sealed in the classes in which they have been inherited. If you want to declare a method as sealed, then it has to be declared as virtual in its base class.

The following class definition defines a sealed class in C#:



In the following code, create a sealed class SealedClass and use it from Program. If you run this code then it will work fine.  




// C# code to define
// a Sealed Class
using System;
 
// Sealed class
sealed class SealedClass {
 
    // Calling Function
    public int Add(int a, int b)
    {
        return a + b;
    }
}
 
class Program {
 
    // Main Method
    static void Main(string[] args)
    {
 
        // Creating an object of Sealed Class
        SealedClass slc = new SealedClass();
 
        // Performing Addition operation
        int total = slc.Add(6, 4);
        Console.WriteLine("Total = " + total.ToString());
    }
}

Output : 

Total = 10

Now, if it tries to inherit a class from a sealed class then an error will be produced stating that ” It cannot be derived from a Sealed class “.




// C# code to show restrictions
// of a Sealed Class
using System;
 
class Bird {
 
}
 
// Creating a sealed class
sealed class Test : Bird {
}
 
// Inheriting the Sealed Class
class Example : Test {
}
 
// Driver Class
class Program {
 
    // Main Method
    static void Main()
    {
    }
}

Error:

Error CS0509 ‘Example’ : cannot derive from sealed type ‘Test’

Consider the following example of a sealed method in a derived class : 




// C# program to
// define Sealed Class
using System;
 
class Printer {
 
    // Display Function for
    // Dimension printing
    public virtual void show()
    {
        Console.WriteLine("display dimension : 6*6");
    }
 
    // Display Function
    public virtual void print()
    {
        Console.WriteLine("printer printing....\n");
    }
}
 
// inheriting class
class LaserJet : Printer {
 
    // Sealed Display Function
    // for Dimension printing
    sealed override public void show()
    {
        Console.WriteLine("display dimension : 12*12");
    }
 
    // Function to override
    // Print() function
    override public void print()
    {
        Console.WriteLine("Laserjet printer printing....\n");
    }
}
 
// Officejet class cannot override show
// function as it is sealed in LaserJet class.
class Officejet : LaserJet {
 
    // can not override show function or else
    // compiler error : 'Officejet.show()' :
    // cannot override inherited member
    // 'LaserJet.show()' because it is sealed.
    override public void print()
    {
        Console.WriteLine("Officejet printer printing....");
    }
}
 
// Driver Class
class Program {
 
    // Driver Code
    static void Main(string[] args)
    {
        Printer p = new Printer();
        p.show();
        p.print();
 
        Printer ls = new LaserJet();
        ls.show();
        ls.print();
 
        Printer of = new Officejet();
        of.show();
        of.print();
    }
}

Output : 

display dimension : 6*6
Printer printing....

display dimension : 12*12
LaserJet printer printing....

display dimension : 12*12
Officejet printer printing....

Explanation: In above C# code, Printer class has display unit with the dimension of 6*6 and LaserJet class have implemented the show method by overriding it to have the dimension of 12*12. If any class will inherit LaserJet class then it will have the same dimension of 12*12 and can’t implement its own i.e. it cannot have 15*15, 16*16 or any other dimensions. So, LaserJet call will seal the show method to prevent further overriding of it.

Why Sealed Classes? 

 


Article Tags :
C#