Understanding Sealed Classes in Java 17: A Comprehensive Guide
Written on
Chapter 1: Introduction to Sealed Classes
Sealed classes represent a significant advancement introduced in Java 16 as a preview feature and officially finalized in Java 17. This feature enables developers to control the inheritance structure of a class or interface, thereby limiting the classes that can extend or implement them.
What Exactly Are Sealed Classes?
Sealed classes allow you to explicitly specify which classes can inherit from a particular class or interface. For example, if we have a class named Shape, we can restrict its subclasses to just Circle, Square, and Triangle. This restriction is achieved through the permits clause in the class definition.
Here's how it looks in code:
public sealed class Shape permits Circle, Square, Triangle {
// Class definition
}
In the snippet above, the Shape class is sealed, allowing only the Circle, Square, and Triangle classes to extend it.
Section 1.1: Defining Sealed Interfaces
You can also define sealed interfaces. For instance, consider the Vehicle interface, which permits only Car and Bike to implement it.
public sealed interface Vehicle permits Car, Bike {
void start();
}
In this case, the Vehicle interface contains a method start(), and only the specified classes can implement this interface.
Subsection 1.1.1: Extending a Sealed Class
To illustrate extending a sealed class, look at the Circle class, which inherits from Shape.
public final class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;}
// Class definition
}
Here, the Circle class successfully extends the sealed class Shape, with a constructor that initializes the radius.
Section 1.2: Attempting Unauthorized Extensions
Now, let's see what happens if we attempt to extend a class that isn’t permitted. For instance, consider the following attempt with Rectangle:
public class Rectangle extends Shape {
private final double width;
private final double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Class definition
}
In this case, since Rectangle is not included in the list of permitted subclasses of Shape, the compiler will throw an error.
Chapter 2: Advantages of Using Sealed Classes
The introduction of sealed classes comes with a variety of benefits:
The first video titled "Sealed Classes in Java | Java 17 features" delves into the features and functionalities of sealed classes, highlighting their significance in modern software development.
Sealed classes help create a finite set of subclasses, thereby establishing a clear hierarchy and preventing unintended behavior in your code. This feature is particularly valuable in large-scale projects, as it minimizes the risk of accidental modifications to critical classes.
The second video, "Java Tutorial - Sealed Classes and Interfaces | JDK 17 (Face Reveal)," provides an engaging exploration of sealed classes and interfaces, offering practical examples and insights.
Benefits of Sealed Classes:
- Enhanced Security: By restricting the inheritance hierarchy, sealed classes prevent unauthorized access to sensitive code sections.
- Better Maintainability: They simplify the code structure, making it easier to understand and modify.
- Improved Performance: The Java Virtual Machine (JVM) can optimize runtime performance due to the predictability of sealed class hierarchies.
Key Points to Remember About Sealed Classes:
- A sealed class must specify its permitted subclasses using the permits clause.
- It must have subclasses listed.
- Only the defined subclasses are allowed to extend the sealed class.
- Permitted subclasses must reside in the same package.
- They should extend their sealed superclass directly.
By leveraging sealed classes, developers can create robust and maintainable codebases that are both secure and efficient.