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.
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.
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.) }
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(); } }
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”.
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.
x = 100 ,y = 180
Sealed classes in C# should be used to:
- 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.
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.
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.
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. }
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.
If you wish to look at all C# Programming examples, go to 1000 C# Programs.
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Check C# Books
- Practice MCA MCQs