| Lesson 4 | Procedural and Data Abstraction in Java SE 22 |
| Objective | Review modern Java SE 22 concepts of abstraction, encapsulation, and ADTs using updated terminology and examples. |
This updated section revisits foundational object-oriented principles—abstraction, encapsulation, and information hiding—using Java SE 22 features such as records, sealed classes, pattern matching, and modules. These updates reflect how abstraction and encapsulation continue to evolve in the modern Java ecosystem.
abstract classes, interfaces, and sealed types, which define clear boundaries for extension. The use of pattern matching for switch further enhances abstraction by allowing type-safe, expressive decision-making without exposing implementation details.
static and default interface methods to encapsulate behavior that can evolve independently of implementation. This approach promotes testable and reusable design patterns, aligning with functional-style programming through Stream operations and lambdas.
records provide a concise syntax for immutable data carriers that automatically encapsulate their fields. For mutable state, developers rely on well-defined accessors and the var keyword for readability without compromising encapsulation.
module-info.java system, preventing unwanted access to internal packages.
record types for immutable transfer objects and sealed interfaces for defining extensible but controlled hierarchies. This ensures that persistence logic remains isolated and replaceable without rewriting higher-level business services.
private and protected access modifiers, this ensures robust API design.
private and provides controlled access through methods or accessor records. In Java SE 22, records automatically generate accessors, and developers can override them for validation.
public record Account(String owner, double balance) {
public Account {
if (balance < 0) throw new IllegalArgumentException("Negative balance not allowed");
}
}
interface with sealed subtypes, allowing controlled extension. This pattern aligns with algebraic data modeling and enhances type safety.
sealed interface Shape permits Circle, Rectangle { }
record Circle(double radius) implements Shape { }
record Rectangle(double width, double height) implements Shape { }
Shape shape = new Circle(5.0);
if (shape instanceof Circle(var r)) {
System.out.println("Area = " + Math.PI * r * r);
}