C# Program to Create a Sealed Class

What is a Sealed Class in C#?

In C#, the sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, this class cannot be inherited.

Purpose of a Sealed Classes

The purpose of sealed classes in C# is to prevent other classes from inheriting from them. This helps maintain class integrity, enhance security, and allow performance optimizations.

Syntax for Declaring a Sealed Class

To create a sealed class in C#, you simply use the sealed modifier when defining the class. The syntax for declaring a sealed class is as follows:

sealed class ClassName
{
    // Class members (fields, properties, methods, etc.)
}
Implementation of Sealed Class

Here is source code of the C# Program to Create Sealed Class. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Create Sealed Class
 */
 
using System;
sealed class SealedClass
{
    public int x;
    public int y;
}
 
class SealedTest
{
    static void Main()
    {
        SealedClass sc = new SealedClass();
        sc.x = 100;
        sc.y = 180;
        Console.WriteLine("x = {0}, y = {1}", sc.x, sc.y);
        Console.ReadLine();
    }
}
Program Explanation

1. We create a sealed class SealedClass with two public integer fields, x and y.
2. In the Main method of the SealedTest class, we create an instance of SealedClass named sc.
3. We set the values of x and y to 100 and 180, respectively, using the dot notation.
4. The program then prints the values of x and y on the console as “x = 100, y = 180”.

advertisement
advertisement

Time Complexity: O(1)
The C# code has constant time complexity (O(1)) because it performs a fixed number of operations, and the execution time does not depend on the input size.

Space Complexity: O(1)
The space complexity is also constant (O(1)) as the code uses a fixed amount of memory for a few variables, regardless of the input.

Program Output:
 
x = 100 ,y = 180
When to use Sealed Classes?

Sealed classes in C# should be used to:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  • Prevent Inheritance: Restrict other classes from inheriting to maintain design integrity and avoid issues with subclassing.
  • Security and Stability: Protect critical functionality, ensuring consistent and secure implementation.
  • Performance Optimization: Allow .NET runtime optimizations due to no further derivations.
  • Clear Class Hierarchy: Maintain a well-defined class structure for better code management.
  • Specific Purpose: Create classes for a specific role without extension, ensuring focused and reliable functionality.
What is Sealed Method?

In C#, a sealed method is a method that cannot be changed or extended by subclasses. It’s marked with the “sealed” keyword, making it the final version of that method in the class hierarchy.

Syntax for Declaring a Sealed Method?

Here’s the syntax for declaring a sealed method in C#:

class BaseClass
{
    public virtual void MyMethod()
    {
        // Method implementation
    }
}
 
class DerivedClass : BaseClass
{
    public sealed override void MyMethod()
    {
        // Sealed method implementation
    }
}

In this example, MyMethod() in the DerivedClass is marked as sealed, preventing any further subclass from overriding it. The BaseClass allows MyMethod() to be overridden, but the DerivedClass seals it, making it the final implementation of the method in the class hierarchy.

advertisement
Implementation of Sealed Method

Here is source code of the C# Program to Create Sealed method. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Create Sealed method
 */
 
using System;
 
class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape.");
    }
}
 
class Circle : Shape
{
    public sealed override void Draw()
    {
        Console.WriteLine("Drawing a circle.");
    }
}
 
class ColoredCircle : Circle
{
    // Error: It's not allowed to override Draw() in ColoredCircle class
    // since it's sealed in Circle class.
}
Program Explanation

1. In this example, we have a base class Shape with a virtual method Draw(). The Circle class is derived from Shape and overrides the Draw() method, sealing it using the sealed keyword.
2. When we try to create a further derived class, ColoredCircle, from Circle and attempt to override the Draw() method, it will result in a compilation error.
3. The error occurs because Draw() is sealed in the Circle class, making it the final implementation, and no more overrides are allowed in subclasses.
4. The sealing of the Draw() method in the Circle class ensures that the specific behavior of drawing a circle remains consistent throughout the class hierarchy, preventing accidental modifications or extensions in further subclasses.

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.