Commit 24d6acd
committed
feat: Add Shoes class with getters and setters for size and color
WHAT the code does:
- Defines a `Shoes` class with private attributes:
- `size` (int)
- `color` (String)
- Provides **setters** (`setSize`, `setColor`) to modify attributes.
- Provides **getters** (`getSize`, `getColor`) to access attributes.
- `main()` demonstrates:
- Creating a `Shoes` object for brown moccasins (size 31).
- Creating another `Shoes` object for black boots (size 32).
- Printing descriptions using getters.
WHY this matters:
- Demonstrates **encapsulation**: private fields exposed via getters and setters.
- Shows how to create and manipulate multiple object instances.
- Reinforces object-oriented thinking: each object holds its own state.
HOW it works:
1. `moccasins` object → `color = brown`, `size = 31`.
2. `boots` object → `color = black`, `size = 32`.
3. Prints:
- `"I have moccasins size 31"`
- `"I also have black boots."`
Tips & gotchas:
- Getters/setters follow JavaBean conventions → useful for frameworks and libraries.
- Could add validation (e.g., `size > 0`) inside setters.
- Overriding `toString()` would allow simpler object display without manual string building.
- Fields are mutable — consider immutability if values should not change once set.
Use-cases:
- Modeling products in an e-commerce or inventory system.
- Teaching encapsulation and object property access in Java.
- Extendable to include brand, price, or material.
- Serves as foundation for **POJOs** (Plain Old Java Objects).
Short key: class-shoes-getters-setters.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e4f5ef9 commit 24d6acd
1 file changed
+2
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
0 commit comments