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.



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


Output :

Total = 10

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()
    {
    }
}

chevron_right


Error:

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}
  
// inherting 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();
    }
}

chevron_right


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 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?

  • Sealed class is used to stop a class to be inherited. You cannot derive or extend any class from it.
  • Sealed method is implemented so that no other class can overthrow it and implement its own method.
  • The main purpose of the sealed class is to withdraw the inheritance attribute from the user so that they can’t attain a class from a sealed class. Sealed classes are used best when you have a class with static members.
    e.g the “Pens” and “Brushes” classes of the System.Drawing namespace. The Pens class represents the pens for standard colors. This class has only static members. For example, “Pens.Red” represents a pen with red color. Similarly, the “Brushes” class represents standard brushes. “Brushes.Red” represents a brush with red color.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.