Illustration of Sealed Classes of C# (C Sharp) with Examples

Being a C# developer, you will surely know and you might have used inheritance in your code.

Sometimes you might also force inheritance to happen by declaring abstract classes. But have you ever tried to avoid inheritance in your class? Rephrasing it, can you protect your class from being inherited? Yes you can do it using sealed classes of C#. This article will help you in understanding sealed classes and in addition it will also let you know where all can the sealed keyword be associated with.

As mentioned above when you declare a class to be sealed, it cannot be inherited. Consider the following example:

public sealed class sampleClass {
private int member1;
public int Prop{
get {
return member1;
}
set {
member1 = value;
}
}
public int squareValue() {
return member1*member1;
}
}
public class testClass {
public static void Main() {
sampleClass obj = new sampleClass();
obj.Prop = 100;
int resultValue = obj.squareValue();
Console.WriteLine(“Square of “+ obj.Prop + “ is:”+resultValue);
}
}

Output of this code will be:

Square of 100 is 1000

This example seems to be usual except that you have introduced the keyword sealed in the class declaration of sampleClass. In this example, you create a sealed class called sampleClass containing a data member and a property to access that data member. You also manipulate on the data member using a member method called squareValue. You then create another class called testClass. This class has Main method inside which you instantiate sampleClass and access its members.

Problem arises when you try to inherit sampleClass. Assume that you are introducing the following class declaration:

public class derivedClass:sampleClass {
public derivedClass(){
Console.WriteLine(“Inside derivedClass”);
}
}

On compiling this code, you will get the following error:

“derivedClass cannot inherit from sealed class sampleClass.”

When you are using sealed classes, your class doesn’t have the chance of getting inherited. Hence during run time, the performance will be faster when compared to the normal classes. Even if you don’t specify explicitly, certain types of C# are implicitly sealed. One good example for it is structs. Structs cannot be inherited, they can only implement interfaces.

So far in the discussion, you have associated the keyword sealed along with a class. Can this keyword be associated anywhere else? Yes. Sealed keyword can be associated with field, property, method and event of a class. What is the purpose of associating sealed keyword with these class members? Assume that you have a derived class inherited from a base class.

Your derived class overrides a method from the base class. You want to ensure that this method should not be overridden by classes which inherit further from the derived class. This is done by specifying the sealed keyword along with the overridden method name. But to do so, you have few design guidelines:

• The class whose members use sealed keyword should be a derived class.
• The method of derived class which is to be sealed should be overridden from the base class and it should be associated with the keyword override.

For better understanding, consider the following example:

class baseClass {
virtual void method1() {
Console.WriteLine(“Executing method1 of baseClass”);
}
virtual void method2() {
Console.WriteLine(“Executing method2 of baseClass”);
}
}
class derivedClass:baseClass {
public sealed override void method1() {
Console.WriteLine(“Executing method1 of derivedClass”);
}
public override void method2() {
Console.WriteLine(“Executing method2 of derivedClass”);
}
}
class testClass {
public static void Main() {
derivedClass obj = new derivedClass();
obj.method1();
obj.method2();
}
}

Output of this code will be:

Executing method1 of derivedClass
Executing method2 of derivedClass

In this example, you have overridden method1 and method2 of baseClass in derivedClass. If derivedClass gets inherited further, you don’t allow the newly derived class to inherit method1. You establish this task by associating sealed keyword along with override keyword in method1 declaration of derivedClass. However method2 of derivedClass can be overridden by any further inheriting classes. You then create an instance of derivedClass and execute method1 and method2 in the Main method of testClass. All these steps are legal and they will work as expected.

Now try to inherit derivedClass and override method1 as shown below:

class derived2Class : derivedClass{
public override void method1() {
Console.WriteLine(“Executing method1 of 2nd level derived class”);
}
public override void method2() {
Console.WriteLine(“Executing method2 of 2nd level derived class”);
}
}

In this example, overriding method2 is legal. But you are also trying to override method1 which is sealed in derivedClass. Hence you will end up in error.

| Design Guideline for C# Structs (C Sharp) | Design Patterns – Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.