Java 15 Sealed Classes: Unleashing the Power of Restricted Inheritance 🚀

beginner
13 min

Java 15 Sealed Classes: Unleashing the Power of Restricted Inheritance 🚀

Welcome to our in-depth guide on Java 15 Sealed Classes! This exciting new feature allows us to create classes that can only be extended by a specific set of permitted subclasses. Let's dive into the world of sealed classes and explore how they can enhance your Java programming skills. 💡

Table of Contents

  1. Introduction to Sealed Classes

    • Understanding the need for Sealed Classes
    • Real-world scenarios where Sealed Classes shine 📝
  2. Creating a Sealed Class

    • Syntax and structure of a Sealed Class
    • Defining permitted subclasses ✅
  3. Restricted Inheritance

    • How sealed classes limit inheritance
    • Ensuring only permitted subclasses can exist 🎯
  4. Compilation and Execution

    • Compiling a Sealed Class program
    • Executing the Sealed Class program ✅
  5. Practical Application

    • A real-world example of using Sealed Classes 📝
  6. Quiz: Test Your Knowledge

    • Put your newfound knowledge to the test 🎯

Introduction to Sealed Classes 📝

Before we dive into Sealed Classes, let's understand why they're important. In traditional Java, classes can be extended by any number of subclasses, leading to potential chaos and confusion. Sealed Classes aim to solve this problem by allowing us to restrict inheritance, ensuring only specific subclasses can exist.

Real-world scenarios where Sealed Classes shine 📝

  1. Enumerated Types: Sealed Classes can be used to create safer and more efficient enumerated types. By limiting the number of permitted subclasses, we can ensure that all possible values are accounted for and avoid runtime errors.
  2. Strategy Pattern: Sealed Classes can be used to implement the Strategy Pattern more effectively. By limiting the number of strategies, we can ensure that only valid strategies are used and reduce the chance of errors.

Creating a Sealed Class ✅

Now, let's create our first Sealed Class. Here's the basic syntax:

java
sealed interface Shape permits Circle, Rectangle { double area(); } record Circle(double radius) implements Shape { @Override public double area() { return Math.PI * Math.pow(radius, 2); } } record Rectangle(double width, double height) implements Shape { @Override public double area() { return width * height; } }

In this example, we have a sealed interface Shape that permits two subclasses: Circle and Rectangle. Both classes implement the area() method as required by the Shape interface.

Restricted Inheritance 🎯

Sealed Classes limit inheritance by ensuring that only permitted subclasses can extend the sealed interface. This means that if we try to extend Shape with an unpermitted subclass, the program will not compile.

java
// This will not compile because Square is not a permitted subclass record Square(double side) implements Shape { //... }

Compilation and Execution ✅

To compile and execute a Sealed Class program, use the javac and java commands as you would with any other Java program.

bash
javac Shape.java Circle.java Rectangle.java Square.java java Shape

Practical Application 📝

Now that you understand the basics of Sealed Classes, let's apply this knowledge to a real-world example. Consider a program that simulates a game of poker. The Card class could be sealed, permitting only specific types of cards like Ace, King, Queen, etc. This ensures that only valid cards can be used in the game, reducing the chance of errors.

Quiz: Test Your Knowledge 🎯

Quick Quiz
Question 1 of 1

What is the purpose of Sealed Classes in Java?

That's it for our guide on Java 15 Sealed Classes! We hope you found it helpful and informative. Keep learning, coding, and creating amazing things with Java! 🚀