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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


//Base class flat from which more flat types
// e.g. 1BHK, 2BHK and 3BHK etc. will be derived.
public class Flat
{
    public void welcome()
    {
        Console.WriteLine("Welcome to Beautiful Apartment...");
    }
    public virtual void display()
    {
        Console.WriteLine("Not ready yet...");
    }
    public virtual void features()
    {
        Console.WriteLine("Not ready yet...");
        balcony();
    }
    public virtual void balcony()
    {
        Console.WriteLine("Not ready yet...");
    }
}

public class OneBHK : Flat
{
    public override void display()
    {
        Console.WriteLine("This is 1 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }
    public override void balcony()
    {
        Console.WriteLine("\tBalcony: Rectangular\n");
    }

}


public class TwoBHK : OneBHK
{
    public override void display()
    {
        Console.WriteLine("This is 2 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }
    public sealed override void balcony()
    {
        Console.WriteLine("\tBalcony: Circular\n");
    }
}


//Mark ThreeBHK class as sealed as no 4 or more BHK
//are allowed in this apartment to build

public sealed class ThreeBHK : TwoBHK
{
    public override void display()
    {
        Console.WriteLine("This is 3 BHK flat");
    }
    public override void features()
    {
        Console.WriteLine("\tFeatures...");
        balcony();
    }

    // We cannot override and implement its own balcony type
    // As we have restriction that all flats above TwoBHK will have circular type
    // and cannot desinged their own type.
    // So, it is sealed in TwoBHK class to get cirucular type balcony only.

    //public override void balcony()
    //{
    //    Console.WriteLine("\tBalcony: Circular\n");
    //}
}


//We cannot extend ThreeBHK class as marked as sealed. 
//In Apartment we don't want to have flats more than Three BHK.

//public class FourBHK : ThreeBHK
//{

//}

Apartment class that will show flats and features.

//Apartment class that will show flats and features.
public class Apartment
{
    Flat f;
    public Apartment(Flat f)
    {
        this.f = f;
    }
    public void displayFlat()
    {
        this.f.welcome();
        this.f.display();
        this.f.features();
    }

}

Test class

//--------------------TEST------------------------------------
namespace sealed_sample
{

    class SealedClassAndMethodTest
    {
        static void Main(string[] args)
        {

            Flat f1bhk = new OneBHK();
            Apartment a = new Apartment(f1bhk);
            a.displayFlat();

            Flat f2bhk = new TwoBHK();
            Apartment b = new Apartment(f2bhk);
            b.displayFlat();

            Flat f3bhk = new ThreeBHK();
            Apartment c = new Apartment(f3bhk);
            c.displayFlat();

        }
    }
}

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.

class Printer
{
    public virtual void display()
    {
        Console.WriteLine("Display dimention: 5x5");
    }
    public virtual void print()
    {
        Console.WriteLine("Printer printing...\n");
    }

}
class LaserJet : Printer
{
    sealed override public void display()
    {
        Console.WriteLine("Display dimention: 10x10");
    }
    override public void print()	
    {
        Console.WriteLine("LaserJet Printer printing...\n");
    }
}

//This OfficeJet class can not overrid display
//function as it is sealed in LaserJet class.

//OfficeJet will have same display
//feature and it cannot implements it's own.So it
//will also have "Display dimention: 10x10".

class OfficeJet : LaserJet
{
    //Can not override display function or else
    //compiler error : 'OfficeJet.display()': cannot override inherited member
    //'LaserJet.display()' because it is sealed.


    //override public void display()
    //{
    //    Console.WriteLine("B.F");
    //}
    override public void print()
    {
        Console.WriteLine("OfficeJet Printer printing...");
    }

}

//--------------------TEST------------------------------------
namespace sealed_sample
{

    class SealedClassAndMethodTest
    {
        static void Main(string[] args)
        {
            Printer p = new Printer();
            p.display();
            p.print();

            Printer l = new LaserJet();
            l.display();
            l.print();

            Printer o = new OfficeJet();
            o.display();
            o.print();

        }
    }
}

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.

Related Posts