Initiative: To distinguish between code that must be extended (abstract) and code that must be protected (final).
abstract class SecurityPolicy { // Abstract class defining a required security blueprint
protected String id; // Attribute identifying the specific policy instance
public SecurityPolicy(String id) { // Constructor for the abstract security policy
this.id = id; // Initialize the policy ID
} // End of constructor
public final void logAccess() { // Final method that children CANNOT override/change
System.out.println("LOG: Access attempt recorded for " + id); // Standard logging logic
} // End of final method
public abstract boolean validate(String token); // Abstract method for custom validation logic
} // End of SecurityPolicy class
class TokenPolicy extends SecurityPolicy { // Subclass implementing token-based security
public TokenPolicy(String id) { // Constructor for the TokenPolicy subclass
super(id); // Pass ID to the parent SecurityPolicy constructor
} // End of constructor
@Override // Override the abstract validate method
public boolean validate(String token) { // Logic to validate a token string
return token != null && token.length() > 5; // Simple rule: token must be > 5 chars
} // End of validate implementation
} // End of TokenPolicy class
final class EncryptionModule { // Final class that CANNOT be inherited by any other class
public void encrypt(String data) { // Method to simulate data encryption
System.out.println("Data encrypted using AES: " + data.hashCode()); // Log hashed result
} // End of encrypt method
} // End of final EncryptionModule class
public class AbstractFinalContract { // Main class demonstrating modifiers
public static void main(String[] args) { // Entry point for the contract test
System.out.println("--- Security Modifier Test ---"); // Print system header
TokenPolicy p = new TokenPolicy("Auth-V1"); // Create a concrete policy object
p.logAccess(); // Call the final method (standard behavior)
boolean res = p.validate("secret123"); // Call the custom overridden method
System.out.println("Validated: " + res); // Print validation result
EncryptionModule em = new EncryptionModule(); // Create instance of the final class
em.encrypt("UserPassword"); // Call method within the final class
System.out.println("--- Test Complete ---"); // Final exit log for the program
} // End of main method
} // End of AbstractFinalContract class