Skip to content

Commit c314c30

Browse files
committed
feat(generics): implement Box<T extends Number> class with bounded type parameter
WHAT: - Created a generic class `Box<T>` that accepts only subclasses of `Number` (e.g., Integer, Double, Float). - Added constructor for initialization, along with getter and setter methods. WHY: - Demonstrates **bounded type parameters** in Java Generics. - Restricts generic types to numeric classes, preventing invalid usages (e.g., Box<String> is not allowed). - Ensures type safety while allowing flexibility with different numeric types. HOW: - Declared class as `Box<T extends Number>`. - Provided constructor `Box(T value)` to initialize the box with a numeric type. - Implemented `getValue()` and `setValue(T value)` for retrieving and modifying the stored value. KEY POINTS: - `T extends Number` means: - `T` can be `Integer`, `Double`, `Float`, `Long`, etc. (all subclasses of `Number`). - Enforces compile-time restriction → avoids misuse with non-numeric types. - Without this bound, `Box<T>` would accept any type (including non-numeric ones). BENEFITS: - Enables safe numeric operations (e.g., `doubleValue()`, `intValue()`) since `Number` defines these methods. - Prevents runtime errors by catching invalid type usage at compile-time. - Makes the class reusable for different numeric scenarios (statistics, math utilities, etc.). REAL-WORLD USE CASES: - Generic mathematical containers that must only store numbers. - Utility classes in frameworks dealing with measurements, coordinates, or statistics. - Similar to how `java.util.Optional<T>` restricts null-handling, this restricts type usage. EXAMPLE USAGE: Box<Integer> intBox = new Box<>(10); Box<Double> doubleBox = new Box<>(15.5); System.out.println(intBox.getValue()); // 10 System.out.println(doubleBox.getValue()); // 15.5 // Box<String> strBox = new Box<>("Hello"); // Compile-time error NOTES: • Upper bounds (extends) are used for reading numeric values. • Lower bounds (super) are used when we want to write values safely. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent b152e85 commit c314c30

File tree

1 file changed

+48
-0
lines changed
  • Section24JavaGenerics/src/GenericConstructors

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package GenericConstructors;
2+
3+
public class Box<T extends Number> {
4+
private T value;
5+
6+
public Box(T value) {
7+
this.value = value;
8+
}
9+
10+
public T getValue() {
11+
return value;
12+
}
13+
14+
public void setValue(T value) {
15+
this.value = value;
16+
}
17+
}
18+
19+
/*
20+
1. Generic Class with Constructor:
21+
- Class `Box<T extends Number>` ek bounded generic class hai.
22+
- Yaha `T` sirf `Number` class ya uske subclasses (Integer, Float, Double, Long, etc.) hi ho sakte hain.
23+
- Ab constructor me `Box(T value)` banaya gaya hai jo object create karte waqt initial value set karega.
24+
25+
2. Example Usage:
26+
Box<Integer> intBox = new Box<>(10); //Allowed
27+
Box<Double> doubleBox = new Box<>(20.5); //Allowed
28+
Box<String> strBox = new Box<>("Hello"); //Error (String is not a Number)
29+
30+
3. Methods:
31+
- `getValue()` → box ke andar jo value hai usko return karta hai.
32+
- `setValue(T value)` → box ke andar naya value set karne deta hai.
33+
34+
4. Benefits of Bounded Generics with Constructor:
35+
- Type safety → sirf numeric types hi box me store honge.
36+
- Compile-time error detection → agar galat type pass karoge (jaise String) toh compiler turant error dega.
37+
- Code reusability → ek hi class alag-alag numeric types (Integer, Float, Double) ke liye use ho sakti hai.
38+
39+
5. Why Constructor is Important?
40+
- Jab a tum object create karte ho, turant ek value assign kar sakte ho.
41+
- Example:
42+
Box<Integer> b1 = new Box<>(100);
43+
System.out.println(b1.getValue()); // Output: 100
44+
45+
✔ `T extends Number` → Sirf numeric subclasses allowed.
46+
✔ Constructor `Box(T value)` → initialization ke time value set karta hai.
47+
✔ Safe, reusable aur strongly typed container ban jata hai numeric values ke liye.
48+
*/

0 commit comments

Comments
 (0)