Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All

Can anyone provide me the real time example of sealed class.
It cannot be inherited. then what is the use to have class like this?



Thanks in advance
Froxy
Posted
Updated 6-Nov-21 1:04am
Comments
Sergey Alexandrovich Kryukov 16-Feb-12 1:58am    
What is "real time"?
--SA

Do you think the only purpose of the class is to be inherited? It it was this way, some classes would never be used! What would be the classes used to create instances? Following you logic, some parent classes with descendants which are never instantiated. Do you feel the absurd of it already?

Just the opposite, most classes used to create instances, are terminal classes, having no descendants. What's wrong with it?

Now, there is no functional difference between sealed or non-sealed terminal classes of any library. We could easily live without "sealed" and have the exact same functionality. "Sealed" is no more then a fool-proof feature. You see, some terminal classes are designed to remain terminal forever. They are used to create instances, but subclassing then just would not make no sense. So, if some "fool" fails to recognize such useless cases (which could be specially indicated in extra sentence in documentation), the compiler error will stop such developer from this useless step. Just an extra convenience and saved developer's time.

(I take "fool" in quotation marks to indicate that this is not a personal characteristics, but a reference to a well-know technical term "fool-proof" meaning "someone who would otherwise be able to do a mistake".)

As to a "real time" example — there are thousands of them. Isn't the use of sealed class reasonable without any examples? In my view, not getting it is nearly the same as not understanding of the notion of "class".

More correct question would be the opposite: why would you think that this or that class needs subclassing? For example, System.AppDomain, http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^]. Subclassing is "programming by extension". So, tell us please how would you extend Application Domain and why? Same thing about many other classes which comes with .NET. Every developer creates many classes like that, too. How about you? :-)

—SA
 
Share this answer
 
Sealed classes cannot be inherited. Like you, I don't know offhand any use case that would justify that a class would need to be sealed, but there it is.

BTW, a struct is sealed without having to use the sealed key word. IMHO, there's little reason to have structs around either, because a class is nothing more than an unsealed struct.

You can also seal virtual methods inside an unsealed class. Confused yet?

Final point - you may not be able to inherit a sealed class, but you can create extension methods for it, thus technically "extending" its functionality. Point of interest - the DateTime object is actually a struct.
 
Share this answer
 
Sealed classes and interfaces restrict which other classes or interfaces may extend or implement them. It is more of a declarative way to restrict the use of a superclass rather than using access modifiers.

The below NumberSystem class is open to all classes, so any subclass can extend it. What if you want to restrict this NumberSystem to a fixed set of subclasses (Binary, Decimal, Octal, and HexaDecimal)?. NumberSystem methods are not for other classes. It means you don’t want any other arbitrary class to extend this NumberSystem class.
class NumberSystem { ... }
final class Binary extends NumberSystem { ... }
final class Decimal extends NumberSystem { ... }
final class Octal extends NumberSystem { ... }
final class HexaDecimal extends NumberSystem { ... }


Using sealed class, you can achieve it by controlling the subclasses that can extend it and prevent any other arbitrary class from doing so. This article provides more details on use case.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900