Commit 11338f6
committed
feat: add Department enum with fields, constructor, and accessor methods
WHAT was implemented:
- Defined an enum `Department` with constants: CS, IT, ECE, CIVIL.
- Each enum constant stores two properties:
- `head` → name of the department head.
- `location` → physical location (block name).
- Added a private constructor to initialize enum-specific fields.
- Added getter methods (`getHeadName()`, `getLocation()`) to safely access properties.
- In `EnumDemo1.main()`, demonstrated usage by printing details of `Department.CIVIL`.
WHY this is important:
- Shows that enums in Java are more powerful than simple constants.
- Enums can encapsulate data and behavior, similar to classes.
- Makes enums highly useful for modeling domain-specific constants with metadata.
KEY CONCEPTS COVERED:
1. **Enum with Fields**
- Unlike plain enums, each constant can hold custom data.
Example: `CS("John", "Block A")`.
2. **Enum Constructor**
- Private by default, ensures no external instantiation.
- Automatically called once per constant.
3. **Methods inside Enum**
- Getters allow controlled access to internal fields.
- Enums can also define custom behavior per constant if needed.
4. **Encapsulation in Enums**
- Enums combine fixed constants with associated attributes.
- Improves readability and reduces hardcoding in business logic.
REAL-WORLD APPLICATIONS:
- **University/College Systems** → Departments with head and location details.
- **Banking/Finance** → Enum of account types with interest rates and policies.
- **E-commerce** → Enum of order statuses with descriptions and workflows.
- **Transport** → Enum of stations/routes with location metadata.
BEST PRACTICES:
- Keep enum fields `private final` (immutable after initialization).
- Provide getters instead of exposing fields directly.
- Use enums instead of parallel arrays or switch-case chains for related constants.
- Enum names → singular (Department), constants → uppercase (CS, IT, CIVIL).
KEYWORDS:
enum with fields, enum constructor, enum methods, encapsulation, department metadata.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8bbe791 commit 11338f6
1 file changed
+14
-17
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
| 10 | + | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
16 | | - | |
| 14 | + | |
| 15 | + | |
17 | 16 | | |
18 | 17 | | |
19 | | - | |
20 | | - | |
| 18 | + | |
| 19 | + | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
24 | 23 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
30 | 27 | | |
31 | 28 | | |
32 | 29 | | |
33 | 30 | | |
34 | | - | |
| 31 | + | |
0 commit comments