โ† Back to list

๐Ÿ›ก๏ธ Sealed Classes in Java: Control Inheritance Like a Pro!

Imagine youโ€™re throwing a party and you only want a select group of friends to join. You donโ€™t want random people crashing in. Well, sealedโ€ฆ

Gowrish ยท 2025-02-20 14:12 ยท 0 claps ยท 2.2 min read paywalled
#java #java-14 #sealed-classes #inheritence
Open on Medium โ†—

๐Ÿ›ก๏ธ Sealed Classes in Java: Control Inheritance Like a Pro!

Imagine youโ€™re throwing a party and you only want a select group of friends to join. You donโ€™t want random people crashing in. Well, sealed classes in Java let you do the same thing โ€” but for your class hierarchy. You control who can extend your classes. Itโ€™s like being the bouncer at a party. ๐ŸŽ‰

Java Sealed Class Features

Java Sealed Class Features

๐Ÿ’ก What Are Sealed Classes?

Sealed classes let you restrict inheritance to specific sub-classes. Just like you only allow your friends to extend your party (I mean, class hierarchy), sealed classes ensure only certain sub classes can extend your class.

public sealed class Animal permits Dog, Cat { }
public final class Dog extends Animal { }
public final class Cat extends Animal { }

If someone tries to extend Animalโ€ฆ

ERROR! ๐Ÿšซ Java stops them right in their tracks. Theyโ€™re not on the guest list!

๐ŸŽฏ Why Use Sealed Classes?

  • Control Inheritance: Only the invited sub classes can join your class hierarchy.
  • Better Readability: You know exactly which sub classes exist โ€” no surprises.
  • Improved Safety: No unwanted sub classes sneaking in and causing chaos. Think of it like security at the party. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

๐Ÿš€ Real-World Example: E-commerce Orders

Letโ€™s say youโ€™re building an e-commerce system with different types of orders: PhysicalOrder, DigitalOrder, and SubscriptionOrder. You donโ€™t want anyone creating random orders!

public sealed class Order permits PhysicalOrder, DigitalOrder, SubscriptionOrder { }
public final class PhysicalOrder extends Order { }
public final class DigitalOrder extends Order { }
public final class SubscriptionOrder extends Order { }

Random subclass trying to sneak in? NOPE.

๐Ÿšซ What Happens If You Try to Extend a Sealed Class?

Hereโ€™s a fun fact: If someone tries to extend a sealed class without being on the guest list, Java will stop them in their tracks.

public class SpecialOrder extends Order { } // Error: Cannot extend sealed class 'Order'

๐Ÿ›  Sealed Interfaces: Bonus Feature!

Just when you thought it couldnโ€™t get any better, sealed interfaces join the party too! You can restrict which classes can implement your interfaces as well.

public sealed interface PaymentMethod permits CreditCard, PayPal { }
public final class CreditCard implements PaymentMethod { }
public final class PayPal implements PaymentMethod { }

And guess what? Only CreditCard and PayPal can implement this interface.

๐ŸŽฏ When to Use Sealed Classes?

  • When you have a fixed set of subclasses. Think about when youโ€™re modeling various states or types like different shapes, or commands.
  • When you want strict control over your class hierarchy (just like controlling who gets into your exclusive club). ๐ŸŽ‰
  • In design patterns like Strategy or State โ€” when you know exactly which types should exist.

๐ŸŽ‰ Conclusion: Unlock the Power of Sealed Classes!

Sealed classes are the VIP bouncer of the Java world. They help you control inheritance, make your code cleaner, and ensure no unauthorized subclasses sneak in. Think of them as your personal security detail for your class hierarchy.

When in doubt, just use sealed classes to keep your code predictable, secure, and error-free. โœŒ๏ธ


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
9bdecd017597
slug
๏ธ-sealed-classes-in-java-control-inheritance-like-a-pro-9bdecd017597
url
https://medium.com/@shankarganes1994/%EF%B8%8F-sealed-classes-in-java-control-inheritance-like-a-pro-9bdecd017597
canonical_url
https://medium.com/@shankarganes1994/%EF%B8%8F-sealed-classes-in-java-control-inheritance-like-a-pro-9bdecd017597
author_url
https://medium.com/@shankarganes1994
status
ok
fetched_at
2026-07-19 21:53:32