Commit 4e638b5
committed
feat(generics): add Box2 class with generic constructor using <T extends Number>
WHAT:
- Implemented `Box2` class with a generic constructor instead of a generic class.
- Constructor `<T extends Number> Box2(T value)` allows initialization with any subclass of `Number`.
- Stored the value in a `Number` field for retrieval.
WHY:
- Demonstrates the concept of **generic constructors** in Java.
- Shows that generics can be applied not only at the class level but also at the constructor level.
- Useful when you want flexibility in object creation without making the whole class generic.
HOW:
- Defined a generic constructor `<T extends Number> Box2(T value)`.
- Stored the passed value in a `Number` type field.
- Added `getValue()` method to retrieve the stored number.
- In `main()`, created examples using `Integer` and `Double`.
KEY POINTS:
- The **class itself is not generic**, only the constructor is.
- This allows multiple numeric types to be accepted during construction, while the stored type is generalized as `Number`.
- `T extends Number` ensures only numeric types (Integer, Double, Float, Long, etc.) can be passed.
BENEFITS:
- Enforces type safety → prevents invalid types like `String`.
- Provides flexibility for initializing with various numeric subclasses.
- Avoids making the whole class generic when only the constructor requires generic behavior.
REAL-WORLD USE CASES:
- Utility classes that process numbers but don’t need to expose generics at the class level.
- Frameworks where flexibility at object creation is required (e.g., mathematical constants, measurement wrappers).
- Logging or serialization helpers where values may be different numeric types.
EXAMPLE USAGE:
```java
Box2 boxInt = new Box2(2); // Integer
Box2 boxDouble = new Box2(3.14); // Double
System.out.println("Box2 int: " + boxInt.getValue()); // 2
System.out.println("Box2 double: " + boxDouble.getValue()); // 3.14
// Box2 boxStr = new Box2("Hello"); // Compile-time error
NOTES:
• Difference vs Generic Class:
• Generic Class: Type parameter applies to the whole class (e.g., Box<T>).
• Generic Constructor: Type parameter applies only to the constructor (class stays non-generic).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c314c30 commit 4e638b5
1 file changed
+51
-0
lines changed| 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 | + | |
0 commit comments