Class Modifiers: Abstract, Final, and Sealed Classes
Explore how Java's class modifiers—abstract, final, and sealed—allow you to control class inheritance and design. Understand their roles in enforcing architecture rules, enabling polymorphism, and securing your Java applications by restricting subclassing and controlling data immutability.
We'll cover the following...
When designing a robust Java application, we must carefully dictate how our classes can be used by other developers. Should the class be a mere template that forces other developers to fill in the blanks? Should it be completely locked down to guarantee security and immutability? Or should it be something securely in between?
Let us explore how class-level modifiers give us strict control over our class architecture.
What is an abstract class, and can it be instantiated?
While abstract classes leave the door wide open for extension, sometimes we need to permanently close that door to protect the integrity of our application.
Why are core Java classes like String marked as final, and what security risks would exist if they weren't?
Historically, Java only offered these two extremes: completely open (abstract) or completely closed (final). Modern Java has introduced a highly requested middle ground.
What are sealed classes and interfaces, and what problem do they solve?
Let's see how the syntax for a sealed class strictly ...