🎯 Advanced OOP — Enum, Date/Time, Exceptions, Lambda
Advanced OOP — Enum, Date/Time: An Enum is a closed set of types that fixes the allowed values up front (`RED, YELLOW, GREEN`), an Exception is a signal that interrupts normal fl
An Enum is a closed set of types that fixes the allowed values up front (`RED, YELLOW, GREEN`), an Exception is a signal that interrupts normal flow and carries the error upward, and a Lambda is a concise syntax that packages a small behavior in one line so you can pass it as a parameter — like a traffic light allowing only three colors (enum), an accident stopping normal traffic and triggering the emergency protocol (exception), and a traffic officer giving a "go" command with a single hand gesture (lambda). But if you could use a plain `String status = "RED"` instead of an Enum, why is an Enum needed? Because with a String, if you accidentally write `"Red"` or `"GREN"` the compiler does not warn you and the bug hides until runtime; an Enum makes an invalid value impossible at compile time. Java's checked-exception requirement forces you to handle errors, unlike Python's entirely optional exception catching; lambdas arrived with Java 8 and resemble TypeScript's arrow functions. For a QA engineer these three directly boost reliability: modeling test states with an Enum prevents typo-driven false PASS, catching the right exception guarantees the test reports the real error, and lambdas keep Stream-based assertions readable and short.
Enum definition and usage
Micro Lab: Code practice
Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.
Why Is an Enum More Than Just a String?
enum Browser { CHROME("chrome")…
`enum Browser { CHROME("chrome"), FIREFOX("firefox"), EDGE("edge"); }` ACCEPTS only 3 values — trying to write `Browser.SAFARI` is a COMPILE-time error, not a runtime one.
Each enum constant can CARRY its own…
Each enum constant can CARRY its own private field (`driver`) and constructor — something a plain `String` constant CANNOT do.
The call Browser.CHROME.getDriver()…
The call `Browser.CHROME.getDriver()` returns "chrome" DIRECTLY, with no typo risk — the chance of accidentally writing `"Chrome"` instead of `"chrome"` DISAPPEARS.
Browser.values() automatically returns…
`Browser.values()` automatically returns ALL enum constants as an array — when a new browser is added, `for` loops that use it cover the new value WITHOUT any code change.
Exceptions — try-catch-finally