Real time example of sealed class and method in C# Programming

Let’s understand the real-time example and use of sealed class and method in C# programming with real estate Apartments and Flat design with C# source code example. There are two examples provided to demonstrate why we use sealed class in C# program with real time example with sourced code i.e. Apartments and Flat design and second one using Printer design.

 

Before moving let’s have a brief note on sealed class and method. You can read detail here about Sealed class and sealed method in C# language. Also, In C# technical job interviews it is asked as when or why do you use sealed class and method in software projects.

Brief note about sealed class and sealed method:

Sealed class is used to stop a class to be inherited. In other word, you cannot derive or extend any class from it. Secondly, Sealed method is used, so that no other class can override it and implement its own method. Don’t get confused that if a method is sealed then the class should also be sealed. We don’t use sealed method in a sealed class. In normal class that is allowed to be inherited, in that class using sealed method make sense.

In real time software projects, we use sealed class and sealed method to stop a developer to extend a class or override a method accidentally. If he does so, C# compiler will flash an error. If he really wants to extend or override he can remove the sealed keyword before class or method.

 

Sealed class real time example – Apartment and Flat design:

We’ll have only 1BHK, 2BHK and 3BHK flat in the Apartment. This is the constraint and more than this i.e. 4BHK or above will not be allowed in this apartment.

Second constraint is 1BHK flat will have rectangular balcony and 2BHK or above will have only circular balcony. Above 2BHK flats cannot have their own balcony design but circular only.

In this design we’ll use multilevel inheritance as some of the features of 1BHK can be inherited in 2 BHK and some 2 BHK features can be inherited in 3 BHK etc.

When we’ll be using sealed class and method in this example

Use of Sealed class – Since, first constraint is that the Apartment can have only up to 3BHK flats. So, we’ll have to mark 3BHK class as sealed. So, that no further extension is possible, means we cannot create 4BHK or above in the apartment.

Use of Sealed method – As per second constraint i.e. 1 BHK has only rectangular balcony and 2BHK or above will have only circular balcony. Also note that 3BHK or above will not be allowed to have their own design (implementation) and will have circular only. means, it will inherit the balcony design of 2BHK.
Hence, we will mark balcony () method as sealed in 2BHK to prevent overriding in 3BHK or above classes.

 

C# source code Example: Read the comments in example itself.

 

Apartment class that will show flats and features.

 

Test class

Sealed class real time example – 2 – Using Printer design:

Let’s have another sealed method and sealed class real time example used in C Sharp application to get good hold on Sealed class and method concept. This example will sound similar as above but it will give more idea.

 

Example description and source code:

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

 

Output:
Display dimention: 5×5
Printer printing…

Display dimention: 10×10
LaserJet Printer printing…

Display dimention: 10×10
OfficeJet Printer printing…

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.