Sealed classes

A sealed class in C# is a class that we do not want to be inherited by any derived class. Once we insert the keyword sealed, a compiler will give a compile-time error if a derived class tries to inherit from the sealed class. The following is the screenshot of the same. For explanation purposes, we will use the same two classes we used in the preceding example:

Note that abstract and sealed do not go hand in hand. abstract means that the class must never be instantiated, whereas the sealed class indicates that the class must never be inherited. Therefore, in hindsight, if we declare a sealed class as abstract, this will not make any sense. Thus, if we do try to declare an abstract class as sealed, we will get a compile-time error, as follows:

In the next section, we will look at another pillar of OOP programming, that is, polymorphism.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.15.161