Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

176. Closing the electrical panel after JDK 17

Do you remember our electrical panel model introduced earlier in Problems 172 and 173? In Problem 173, we closed this model as much as possible by using the Java capabilities available before JDK 17. Now, we can revisit that model (Problem 173) and close it completely via JDK 17 sealed classes.

We start with the ElectricComponent interface, which is declared as follows:

public interface ElectricComponent {}

At this moment, this interface is not closed. It can be extended/implemented from any other point of the application. But we can close it by transforming it into a sealed interface with the proper permits clause, as follows:

public sealed interface ElectricComponent
  permits ElectricCircuit, ElectricBreaker, 
          Capacitor, Resistor, Transistor {}

Next, let’s focus on the semi-closed ElectricCircuit class. This is an abstract class that uses a package-private constructor to block any extension from...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard