oop

6. Final: Preventing Inheritance & Overriding

Analyze the code, write your predicted output, and then reveal the solution.

Scenario

What happens if we try to extend a final class or override a final method? (Theoretical since it won't compile, but important for concepts).

Code Block
final class SecurityConfig { final void validate() { System.out.println("Standard Validation"); } } // class HackConfig extends SecurityConfig { } // COMPILE ERROR: Cannot inherit from final class Base { final void secret() { System.out.println("Base Secret"); } } class Derived extends Base { // void secret() { } // COMPILE ERROR: Cannot override final method } public class Main { public static void main(String[] args) { new Base().secret(); } }
Take Your Shot
Predicted Output

Type exactly what you think will be printed in the console.

6. Final: Preventing Inheritance & Overriding | DevExCode