Use of Sealed class and method in C# application

Answer: We’ll be discussing one more use of Sealed class and method in C# application to get good hold on Sealed class concept. In C# technical job interview it is asked as when or why do you use sealed class and method in software projects.

One of the real time use of Sealed class and method in C# has been already explained.

Example

In below example, Printer class has display unit with dimention of 5×5 and LaserJet class have implemented display method by overriding it to have dimention of 10×10. If any class is going to inherit LaserJet class that will have to have same dimention of 10×10 and can not implements its won, means cannot have 15×15, 16×16 or anything. So, LaserJet call will seal the display method to prevent further overriding of it.

 

If we want to prevent further extension of a class for example, OfficeJet class here, we can use sealed keyword before it. For example “sealed class OfficeJet{}”. Now if we try to extend it we will get compiler error.