Skip to content

Commit 9a63eb1

Browse files
committed
feat: implement bounded type parameters in a generic Box class
WHAT: - Added `Box<T extends Number>` class in `BoundedTypesinInterfaces` package. - Created `Main.java` to demonstrate usage of bounded generics. - Restricted type parameter `T` to subclasses of `Number` (e.g., Integer, Float, Double). - Showed compile-time error when attempting to use a non-Number type (e.g., String). HOW: 1. `Box<T extends Number>`: - Declares a generic class where `T` must be a subclass of `java.lang.Number`. - Ensures only numeric types can be stored in the `Box`. - Provides `getValue()` and `setValue()` methods to access and modify the value. 2. `Main.java`: - Demonstrates a valid instantiation with `Box<Integer>`. - Commented example `Box<String>` shows compile-time restriction (invalid type). WHY: - Enforcing bounds (`T extends Number`) ensures that only valid numeric types are allowed. - Prevents misuse of the class with incompatible data types. - Improves type safety and readability by making constraints explicit. REAL-WORLD APPLICATIONS: - Bounded generics are useful in mathematical utility classes, statistical libraries, or any domain where only numeric data makes sense. - For example, implementing a generic `Calculator<T extends Number>` or `Statistics<T extends Number>`. NOTES: - `Box<Integer>`, `Box<Double>`, and `Box<Float>` work correctly. - `Box<String>` fails at compile-time, avoiding runtime errors. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 37f4d04 commit 9a63eb1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package BoundedTypesinInterfaces;
2+
3+
public class Box<T extends Number> {
4+
private T value;
5+
6+
public T getValue() {
7+
return value;
8+
}
9+
10+
public void setValue(T value) {
11+
this.value = value;
12+
}
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package BoundedTypesinInterfaces;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
Box<Integer> box = new Box<>();
6+
//Bounded type parameter laga deya hai.
7+
8+
//Box<String> box = new Box<String>();
9+
//remove comment and check error.
10+
}
11+
}
12+
13+
/*
14+
1. Generic Class `Box<T extends Number>`:
15+
- Yaha `T` ek **bounded type parameter** hai.
16+
- Matlab `T` sirf `Number` class ya uske subclasses (Integer, Double, Float, Long, etc.) hi ho sakte hain.
17+
- Example: `Box<Integer>`, `Box<Double>` valid hai.
18+
- Example: `Box<String>` error dega, kyunki String Number ka subclass nahi hai.
19+
20+
2. Why Bound Generics?
21+
- Jab tumhe generics ke andar sirf specific type hierarchy allow karni ho.
22+
- Example: Agar tumhe mathematical calculations karni hain → toh sirf `Number` aur uske subclasses ka support chahiye.
23+
24+
3. getValue() / setValue():
25+
- `getValue()` → box ke andar ka value return karta hai.
26+
- `setValue(T value)` → box ke andar ek naya value set karne deta hai.
27+
- Type safety maintain hoti hai, kyunki `T` fixed hai (Integer, Double etc.).
28+
29+
4. Main Class:
30+
- `Box<Integer> box = new Box<>();` → sahi hai (Integer extends Number).
31+
- Agar `Box<String>` try karoge toh compile-time error aayega.
32+
- Error: "Type parameter String is not within its bound".
33+
34+
✔ `T extends Number` → bounded generics, sirf Number aur uske subclasses allowed.
35+
✔ Compile-time safety: galat type (jaise String) detect ho jaati hai.
36+
✔ Useful jab tumhe generics me type restriction enforce karna ho.
37+
*/

0 commit comments

Comments
 (0)