• 🛡️ Sealed Classes

    Carrefour Shop Edition 🛒

    In Java, sealed classes and interfaces let you control which classes are allowed to extend or implement them.
    This is useful when you want to model a specific domain with strict rules on how the class hierarchy should look.

    🧠 Why Use Sealed Classes?

    Normally, inheritance is about code reuse. But sometimes, you're modeling a system where you want to limit the possibilities.

    Let’s say you’re building an app for Carrefour, and you want to represent the different store types they operate.
    You don’t want someone randomly creating a CarrefourRandomStore that doesn’t make sense in your domain.

    That’s where sealed classes come in. They allow you to define exactly which subclasses are valid.

    🔧 Declaring a Sealed Class

    Use the sealed modifier, followed by a permits clause to list the allowed subclasses.

    🧾 Example – CarrefourShop.java

    Here, only CarrefourCity, CarrefourMarket, and CarrefourHyper are allowed to extend CarrefourShop.

    🏬 CarrefourMarket.java

    non-sealed means other classes can extend this one. For example, you could later add CarrefourMarketUrban or CarrefourMarketSuburb.

    🛍️ CarrefourHyper.java

    This one is sealed again, and limits its subclasses to only CarrefourDrive and CarrefourBio.

    🔄 Additional Subclasses

    🚗 CarrefourDrive.java

    🌱 CarrefourBio.java

    🧾 Alternative: Defining Everything in One File

    If all permitted subclasses are declared in the same file, you can omit the permits clause:

    ✅ Benefits of Sealed Classes

    🟣 You explicitly define valid subtypes.

    🟣 Your class hierarchy becomes easier to reason about.

    🟣 Great support for exhaustive switch expressions in newer Java versions.