How do I use sealed classes to control inheritance?

In Java, sealed classes are a feature introduced in Java 15 (as a preview and finalized in Java 17) that allows you to control inheritance by specifying which classes or interfaces can extend or implement a given class or interface. This makes your class hierarchy more predictable and easier to reason about.

Using sealed classes involves the following key concepts:

1. Declaration of a Sealed Class

A class can be declared as sealed, which means that only a specific set of classes (declared permits) can extend that class. Here’s the basic syntax:

public sealed class ParentClass permits ChildA, ChildB {
    // Class code
}

Here, only ChildA and ChildB (declared in permits) are allowed to extend ParentClass. This ensures complete control over the inheritance structure of your class.


2. The Role of Permitted Subclasses

Each subclass specified in the permits clause must do one of the following to complete the sealed hierarchy:

  • Declare itself as final (no further inheritance is allowed).
  • Declare itself as sealed (allowing further controlled inheritance).
  • Declare itself as non-sealed (allowing unrestricted inheritance).

Examples of each:

Final Subclass:

public final class ChildA extends ParentClass {
    // Class code
}

Sealed Subclass:

public sealed class ChildB extends ParentClass permits GrandChild {
    // Class code
}

public final class GrandChild extends ChildB {
    // Class code
}

Non-Sealed Subclass:

public non-sealed class ChildC extends ParentClass {
    // Class code
}

In the case of non-sealed, ChildC and its subclasses can be freely inherited, bypassing the restrictions of sealing.


3. Key Features and Benefits of Sealed Classes

  1. Ensure Complete Class Hierarchy Control:
    • By listing all allowed subclasses, you can restrict who can build upon your functionality.
    • Simplifies reasoning about the class hierarchy in complex systems.
  2. Improved Exhaustiveness Checking:
    • When used with instanceof or switch expressions, the compiler knows all the possible subclasses (because they’ve been explicitly listed).
    • For example, pattern matching with switch:
    public String process(ParentClass obj) {
         return switch (obj) {
             case ChildA a -> "ChildA";
             case ChildB b -> "ChildB";
             default -> throw new IllegalStateException("Unexpected value: " + obj);
         };
     }
    
  3. Enforces Encapsulation and API Design Consistency:
    • Encourages developers to think hard about which subclasses make sense.
  4. Useful for Modeling Closed Systems:
    • Great for scenarios where the possible subclasses represent a closed set of types, such as states in a state machine.

Example: Sealed Class for a Shape Hierarchy

Here is a practical example of using sealed classes in a geometric shape hierarchy:

public sealed class Shape permits Circle, Rectangle, Square {
    // Common shape fields and methods
}

public final class Circle extends Shape {
    // Circle-specific fields and methods
}

public final class Rectangle extends Shape {
    // Rectangle-specific fields and methods
}

public final class Square extends Shape {
    // Square-specific fields and methods
}

If someone tries to create a new subclass of Shape outside of those specified in permits, a compilation error will occur.


4. Rules and Restrictions

  • A sealed class must use the permits clause unless all permitted implementations are within the same file.
  • The permitted classes must extend the sealed class or implement the sealed interface.
  • Subclasses of sealed classes located in different packages must be public.
  • All permitted classes are resolved at compile time.

Summary

Java’s sealed classes provide you with a powerful tool to control inheritance in your programs by explicitly defining the classes that are allowed to extend or implement a particular class or interface. They make your code more robust, predictable, and maintainable by restricting which subclasses can exist in a hierarchy. Use them when you want tight control over a class hierarchy or when modeling scenarios with a limited set of possibilities.

How do I use enhanced instanceof pattern matching?

Enhanced instanceof pattern matching, introduced in Java 16 (as a preview feature) and finalized in Java 17, allows you to combine type checking with type casting, reducing boilerplate code and making it more concise and readable.

Here’s how you can use enhanced instanceof pattern matching:

  1. Basic Usage:
    Instead of separately checking if an object is an instance of a class and then casting it, you can do both in one step using the pattern matching feature. The syntax is:

    if (obj instanceof Type variableName) {
       // variableName is automatically cast to Type
    }
    

    Example:

    Object obj = "Hello, Java!";
    
    if (obj instanceof String str) { // This checks and casts obj to String
       System.out.println("String length: " + str.length());
    } else {
       System.out.println("Not a string.");
    }
    

    This eliminates the need for explicit type casting.

  2. Combine with Logical Operators:
    You can combine the pattern matching with additional conditions using logical operators like && or ||.

    Example:

    Object obj = "Patterns in Java";
    
    if (obj instanceof String str && str.length() > 10) {
       System.out.println("String is longer than 10 characters: " + str);
    } else {
       System.out.println("String is too short or not a string at all.");
    }
    

    In this case, the str variable is only in scope if both conditions are true.

  3. Scope of the Pattern Variable:

    • The pattern variable (e.g., str in the examples above) is only accessible within the block where the pattern matching is true.
    • Outside of the if block, the variable doesn’t exist.
  4. Negating with !instanceof:
    Pattern matching itself cannot be negated directly (no “not instanceof”), but you can invert the condition like this:

    if (!(obj instanceof String)) {
       System.out.println("Not a string.");
    }
    
  5. Using Pattern Matching in switch:
    Starting from Java 17 (as a preview) and improved in later versions, you can use pattern matching in switch statements for more powerful expressions. For example:

    Object obj = "Java 17";
    
    switch (obj) {
       case String str && str.length() > 5 -> System.out.println("Long string: " + str);
       case String str -> System.out.println("Short string: " + str);
       default -> System.out.println("Not a string.");
    }
    

    This allows a combination of pattern matching and conditionals directly within switch.


Benefits of Enhanced instanceof Pattern Matching

  • Reduction of Boilerplate Code: By avoiding explicit casting and declaring new variables.
  • Improved Readability: Simplifies conditional checks by combining the instance check and cast in one step.
  • Type Safety: Provides better compile-time safety for the variables you use after a cast.

Recap of the Code Features

From the files you’ve referenced:

  1. PatternMatchingExample.java demonstrates simple pattern matching with instanceof, where the type check and assignment are done in one step.
  2. PatternMatchingExampleCombine.java shows combining pattern matching with additional conditions (e.g., &&).

Both examples illustrate the practical and concise approach to type checking and casting introduced via enhanced instanceof pattern matching.

How do I use Optional for cleaner null checks?

Using Optional in Java can help streamline and simplify null checks, avoiding potential NullPointerException issues and making the code more readable and elegant. Optional is particularly useful when you want to express the possibility of an absent value explicitly in the API and handle such scenarios gracefully.

Here’s how you can use Optional for cleaner null checks:


1. Creating an Optional

You can create an Optional object to wrap either a non-null or null value.

Optional<String> optionalValue = Optional.of("example"); // Non-null value
Optional<String> emptyOptional = Optional.empty();       // Explicit empty optional
Optional<String> nullableOptional = Optional.ofNullable(null); // Can be null

2. Using isPresent() for Checks

Instead of if (value != null), you can use isPresent() to check for a value’s presence:

Optional<String> optionalValue = Optional.ofNullable("example");
if (optionalValue.isPresent()) {
    System.out.println("Value is present: " + optionalValue.get());
}

3. Using ifPresent() for Action

If you want to perform some operation only if a value is present, you can use ifPresent():

optionalValue.ifPresent(value -> System.out.println("Found: " + value));

This eliminates the need for explicit if checks.


4. Provide a Default Value with orElse()

You can supply a default value to use if the Optional is empty:

String result = optionalValue.orElse("Default Value");
System.out.println(result);

5. Lazy Default Value with orElseGet()

To defer the computation of the default value:

String result = optionalValue.orElseGet(() -> "Generated Default");
System.out.println(result);

6. Throw an Exception if Absent with orElseThrow()

You can ensure an exception is thrown when the value is absent:

String value = optionalValue.orElseThrow(() -> new IllegalArgumentException("Value is missing!"));

7. Transforming the Value with map()

Use map() to apply a transformation function to the contained value, without needing to check for null:

Optional<Integer> length = optionalValue.map(String::length);
length.ifPresent(len -> System.out.println("Length: " + len));

8. Chained Operations with flatMap()

If the transformation itself returns an Optional, use flatMap() to avoid nesting:

Optional<String> toUpperCaseOptional = optionalValue.flatMap(value -> Optional.of(value.toUpperCase()));
toUpperCaseOptional.ifPresent(System.out::println);

9. Filtering Values

You can filter the value based on a condition:

optionalValue.filter(value -> value.length() > 5)
             .ifPresent(value -> System.out.println("Value with sufficient length: " + value));

10. Combining Operations

Combine operations like map, filter, and orElse to handle cases cleanly in a pipeline:

String finalValue = optionalValue
                        .map(String::toUpperCase)
                        .filter(value -> value.startsWith("EX"))
                        .orElse("Default Result");
System.out.println(finalValue);

Common Use Cases:

  • Avoid nullable parameters in methods by using Optional.
  • Indicate that a return value may or may not be present, eliminating null checks on the client side.
  • Use in streams to safely process values.

By following these practices with Optional, you can reduce boilerplate code and improve the overall clarity of null safety in Java applications.

How do I handle null safely using Objects.requireNonNullElse?

The Objects.requireNonNullElse method, introduced in Java 9, provides a safe and convenient way to handle null references by returning a default value if the provided object is null. This method ensures that you won’t get a NullPointerException in cases where you expect an object but want a fallback when it’s null.

Syntax

public static <T> T requireNonNullElse(T obj, T defaultObj)

Parameters

  • obj: The object to check for null.
  • defaultObj: The object to return if obj is null. This cannot be null; otherwise, a NullPointerException will be thrown.

Returns

  • If obj is not null, it returns obj.
  • If obj is null, it returns defaultObj.

Key Features

  • Ensures defaultObj is never null. If you pass a null defaultObj, the code will throw a NullPointerException.
  • Useful when you want a non-null value without writing explicit if-else conditions.

Example Usage

import java.util.Objects;

public class Main {
    public static void main(String[] args) {
        String value = null;
        String defaultValue = "Default Value";

        // Using Objects.requireNonNullElse
        String result = Objects.requireNonNullElse(value, defaultValue);

        // Prints: Default Value
        System.out.println(result);

        // If value is not null
        value = "Actual Value";

        // Prints: Actual Value
        System.out.println(Objects.requireNonNullElse(value, defaultValue));
    }
}

How It Works

  1. When value is null, Objects.requireNonNullElse(value, defaultValue) will safely return "Default Value".
  2. When value is not null, it returns the actual value of value.

Important Notes

  1. defaultObj cannot be null:
    If the defaultObj provided is null, the method will throw a NullPointerException. For example:

    String result = Objects.requireNonNullElse(null, null); // Throws NullPointerException
    
  2. Use for Non-Primitive Types Only:
    Since Objects.requireNonNullElse works only with reference types (i.e., not primitive types like int, double), use boxed primitives such as Integer, Double, etc., when needed.

    // Example with Integer:
    Integer number = null;
    Integer defaultNumber = 42;
    
    Integer result = Objects.requireNonNullElse(number, defaultNumber);
    
    // Prints: 42
    System.out.println(result);
    

Using Objects.requireNonNullElse is a clean, concise, and safe way to provide fallback values for potentially null objects without the need for verbose checks.

How do I use switch expressions introduced in Java 14+?

The switch expression, introduced in Java 12 (as a preview feature) and became a standard feature in Java 14, provides a more concise and powerful way to use switch statements. Here’s how to use it effectively:

Key Features of Switch Expressions

  1. Simpler Syntax: The new syntax allows the use of the -> syntax to eliminate fall-through behavior.
  2. Expression Form: The switch can now return a value directly.
  3. Multiple Labels: Multiple case labels can share the same logic using a comma-separated list.
  4. No More Breaks: No need for the break keyword after each case.

Syntax for Switch Expressions

Here’s a quick breakdown:

String dayType = switch (dayOfWeek) {
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Weekday";
    case "Saturday", "Sunday" -> "Weekend";
    default -> throw new IllegalArgumentException("Invalid day: " + dayOfWeek);
};

Explanation:

  • The -> syntax replaces the colon and break of the traditional switch.
  • default acts as a fallback for unmatched cases.
  • The result of the switch is assigned directly to the variable dayType.
  • Multiple cases separated by commas handle identical conditions.

Examples of Switch Expressions

Return a Value Directly from switch

int month = 3;
int daysInMonth = switch (month) {
    case 1, 3, 5, 7, 8, 10, 12 -> 31;
    case 4, 6, 9, 11 -> 30;
    case 2 -> 28; // Use 29 for leap years, this is simplified.
    default -> throw new IllegalArgumentException("Invalid month: " + month);
};
System.out.println("Days in Month: " + daysInMonth);

Using Code Blocks in a Case

For more complex logic, you can use curly braces {} to group multiple statements into a block. In such cases, you must use the yield keyword to specify a value to be returned.

String grade = "B";
String feedback = switch (grade) {
    case "A", "B" -> "Great job!";
    case "C", "D" -> {
        System.out.println("Encouraging message for grade: " + grade);
        yield "Needs improvement.";
    }
    case "F" -> "Failed.";
    default -> throw new IllegalArgumentException("Unknown grade: " + grade);
};
System.out.println("Feedback: " + feedback);

Advantages Over Traditional switch

  1. No Fall-Through: Avoid accidentally executing multiple cases (common bug with traditional switch).
  2. Cleaner Syntax: Easier to read and write due to the arrow operator (->) and elimination of break.
  3. Enhanced Type Safety: The returned value must match the expected type assigned to the variable.
  4. Pattern Matching (Java 17+): Future extensions allow switch with pattern matching for richer capabilities.

Use Cases

  1. Assigning values directly with clear logic.
  2. Simplifying code structure for multiple conditions or enums.
  3. Handling complex branching logic.