Commit 495e93b
committed
feat: Add DemoDataHidding with encapsulated fields and validation for rectangle properties
WHAT the code does:
Defines DemoDataHidding with private fields length and breadth.
Provides public getters and setters:
- getLength(), getBreadth() return field values.
- setLength(double l), setBreadth(double b) assign values, defaulting to 0 if negative.
Implements methods:
- area(): computes length × breadth.
- perimeter(): computes 2 × (length + breadth).
- isSquare(): returns true if length == breadth.
Defines DataHidding with main():
- Attempts to create a Rectangle object, set dimensions, and print computed values.
WHY this matters:
Demonstrates data hiding by using private fields with controlled access through getters and setters.
Validates inputs to prevent invalid states (e.g., negative dimensions).
Models real-world rectangle properties with simple methods for area, perimeter, and square check.
Highlights encapsulation as a core OOP principle.
HOW it works:
When positive values are set via setters, the fields are updated normally.
If negative values are provided, the setters assign 0 instead.
area() and perimeter() compute rectangle geometry.
isSquare() compares length and breadth for equality.
Tips and gotchas:
The DataHidding class references Rectangle, which does not exist; it should reference DemoDataHidding instead.
Comparing doubles with == in isSquare() may be unsafe due to floating-point precision issues; use a tolerance (e.g., Math.abs(a-b) < 1e-9).
Encapsulation could be further strengthened by using constructors for required fields instead of relying only on setters.
Overriding toString() would make printing object details cleaner.
Use-cases:
Educational example of encapsulation and data hiding in Java.
Demonstrates geometry modeling with validation.
Foundation for building robust shapes and geometry classes in larger applications.
Short key: class-demodatahidding encapsulation rectangle-validation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 104f008 commit 495e93b
1 file changed
+11
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | | - | |
7 | | - | |
| 5 | + | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
12 | | - | |
| 9 | + | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
16 | | - | |
17 | | - | |
| 13 | + | |
18 | 14 | | |
19 | 15 | | |
20 | 16 | | |
21 | 17 | | |
22 | 18 | | |
23 | 19 | | |
24 | | - | |
25 | | - | |
| 20 | + | |
26 | 21 | | |
27 | 22 | | |
28 | 23 | | |
29 | 24 | | |
30 | 25 | | |
31 | 26 | | |
32 | | - | |
33 | | - | |
| 27 | + | |
34 | 28 | | |
35 | 29 | | |
36 | 30 | | |
37 | | - | |
38 | | - | |
| 31 | + | |
39 | 32 | | |
40 | 33 | | |
41 | 34 | | |
42 | | - | |
43 | | - | |
| 35 | + | |
44 | 36 | | |
45 | 37 | | |
46 | 38 | | |
47 | 39 | | |
48 | 40 | | |
49 | 41 | | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
| 42 | + | |
55 | 43 | | |
56 | 44 | | |
57 | | - | |
| 45 | + | |
58 | 46 | | |
59 | 47 | | |
60 | 48 | | |
| |||
64 | 52 | | |
65 | 53 | | |
66 | 54 | | |
67 | | - | |
| 55 | + | |
0 commit comments