Illustration of Sealed Classes of C# (C Sharp) with ExamplesBeing 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. public sealed
class sampleClass { 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 { 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 doesnt have the chance of getting inherited. Hence during run time, the performance will be faster when compared to the normal classes. Even if you dont 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. For better understanding, consider the following example: class baseClass
{ Output of this code will be: Executing
method1 of derivedClass In this example, you have overridden method1 and method2 of baseClass in derivedClass. If derivedClass gets inherited further, you dont 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{ 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.
|