Commit df2f9bb
committed
feat: Add Student2 class with encapsulation, validation, and constructors
WHAT the code does:
Defines Student2 with private fields:
- rollNo, name, age, and course.
Implements:
- Default constructor: initializes with safe default values.
- Parameterized constructor: uses setters for validation during initialization.
Provides getters and setters with validation:
- rollNo must be positive.
- name and course must not be null or empty.
- age must be between 16 and 60.
Adds getDetails() to return formatted student information.
Defines GetAndSetMethodSolve with main():
- Demonstrates object creation via default and parameterized constructors.
- Updates properties with setters.
- Tests validation by providing invalid inputs.
- Prints full details and individual properties using getters.
WHY this matters:
Encapsulates object data for safety and consistency.
Introduces input validation to enforce domain-specific rules (e.g., age range, positive roll number).
Demonstrates constructor overloading for flexible object creation.
Shows practical usage of getters and setters beyond simple boilerplate by embedding validation logic.
HOW it works:
s1 is created with default values, then updated using setters.
s2 is created directly with valid values through the parameterized constructor.
s3 demonstrates validation: invalid inputs trigger error messages, leaving defaults unchanged.
getDetails() prints the internal state in a human-readable format.
Tips and gotchas:
Validation currently only prints warnings; in real systems, throwing exceptions or rejecting construction might be better.
Using immutable design (final fields, constructor-only initialization) can eliminate the need for setters in some cases.
Overriding toString() instead of using getDetails() would integrate better with Java’s object system.
Validation logic is basic; could be extended with regex for names, or course catalogs for valid courses.
Use-cases:
Educational example of encapsulation, constructors, and validation in Java.
Foundation for academic systems like student records or enrollment systems.
Demonstrates robust setter/getter design beyond trivial access.
Short key: class-student2 encapsulation validation constructor-overloading.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e1f3ab4 commit df2f9bb
File tree
1 file changed
+122
-1
lines changed- Section11ObjectOrientedProgramming/src
1 file changed
+122
-1
lines changedLines changed: 122 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
1 | 94 | | |
2 | 95 | | |
3 | | - | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
4 | 125 | | |
5 | 126 | | |
0 commit comments