Sobes.tech
Junior — Middle

Kas saaksite anda näidet final modifikaatori kasutamisest Java klassi deklareerimisel?

sobes.tech AI

Vastus AI-lt

Declaring a class in Java with the final modifier means that this class cannot be inherited. This is useful when you want to prevent the class from being extended to preserve its behavior or security.

Example:

public final class Constants {
    public static final String APP_NAME = "MyApp";
    // Other constants
}

// Attempting to inherit will cause a compilation error
// public class ExtendedConstants extends Constants {} // Error

In this example, the class Constants is declared as final to prevent inheritance and modification of its behavior.