Commit 61cf64e
committed
feat: Add Encapsulation class to demonstrate private fields with getters and setters
WHAT the code does:
Defines an Encapsulation class with private attributes:
- color, brand, model, countryOrigin, year, speed.
Implements behavior methods:
- accelerate(int increment): increases speed.
- brake(int decrement): decreases speed but prevents it from going below zero.
Provides getter and setter methods for all attributes to control access.
main() demonstrates usage by:
- Setting attributes via setters.
- Updating speed with accelerate and brake.
- Printing details via getters.
WHY this matters:
Illustrates encapsulation, a core principle of object-oriented programming:
- Fields are hidden (private) and accessed through controlled interfaces (getters/setters).
- Prevents direct modification of internal state, enforcing better data integrity.
Shows how encapsulation separates representation (fields) from interaction (methods).
HOW it works:
In main():
- A car object is created and initialized with setters (brand = Tesla, model = Model S, etc.).
- accelerate(50) sets speed to 50.
- brake(20) reduces speed to 30.
- Getter methods retrieve field values for display.
Tips and gotchas:
Encapsulation is stronger when setters include validation (e.g., rejecting negative year or unrealistic speeds).
Overusing setters/getters without logic reduces them to boilerplate; consider immutability or records for simpler cases.
The toString() method could be overridden to simplify object state printing instead of multiple getter calls.
For real-world systems, builders or constructors may be preferable for setting required fields.
Use-cases:
Educational demonstration of encapsulation in Java.
Useful for modeling entities where state must be controlled and validated.
Foundation for larger systems applying OOP principles like abstraction and inheritance.
Short key: class-encapsulation private-fields getters-setters oop.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 8a1de8b commit 61cf64e
File tree
1 file changed
+59
-39
lines changed- Section11ObjectOrientedProgramming/OOPs 2.O/src
1 file changed
+59
-39
lines changedLines changed: 59 additions & 39 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
5 | 5 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
15 | 14 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
| 25 | + | |
21 | 26 | | |
| 27 | + | |
22 | 28 | | |
23 | | - | |
| 29 | + | |
24 | 30 | | |
25 | | - | |
26 | 31 | | |
27 | | - | |
| 32 | + | |
28 | 33 | | |
29 | 34 | | |
30 | 35 | | |
31 | 36 | | |
32 | 37 | | |
33 | | - | |
34 | 38 | | |
35 | 39 | | |
36 | 40 | | |
37 | 41 | | |
38 | 42 | | |
39 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | | - | |
| 50 | + | |
44 | 51 | | |
45 | | - | |
46 | 52 | | |
47 | | - | |
| 53 | + | |
48 | 54 | | |
49 | 55 | | |
50 | 56 | | |
51 | 57 | | |
52 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
53 | 62 | | |
54 | 63 | | |
55 | 64 | | |
56 | 65 | | |
57 | | - | |
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | 69 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
73 | 73 | | |
74 | | - | |
75 | | - | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
0 commit comments