Skip to content

Commit 24d6acd

Browse files
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

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Section10Methods/Methods 2.O/src/Shoes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
public class Shoes {
2-
// Instance attributes
2+
// Instance attributes.
33
private int size;
44
private String color;
55

6-
// Setters
6+
// Setters.
77
public void setSize(int size) {
88
this.size = size;
99
}

0 commit comments

Comments
 (0)