Skip to content

Commit 4e638b5

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

File tree

1 file changed

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

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package GenericConstructors;
2+
3+
public class Box2 {
4+
private Number value;
5+
6+
// Generic constructor
7+
public <T extends Number> Box2(T value) {
8+
this.value = value;
9+
}
10+
11+
public Number getValue() {
12+
return value;
13+
}
14+
15+
public static void main(String[] args) {
16+
Box2 boxInt = new Box2(2); //Integer
17+
Box2 boxDouble = new Box2(3.14); //Double
18+
19+
System.out.println("Box2 int: " + boxInt.getValue());
20+
System.out.println("Box2 double: " + boxDouble.getValue());
21+
}
22+
}
23+
24+
/*
25+
1. Non-Generic Class + Generic Constructor:
26+
- Class `Box2` khud generic nahi hai (iska type parameter <T> nahi hai).
27+
- Lekin isme ek **generic constructor** banaya gaya hai:
28+
public <T extends Number> Box2(T value)
29+
- Matlab: constructor ko call karte waqt tum koi bhi `Number` subclass (Integer, Double, Float, Long, etc.)
30+
pass kar sakte ho.
31+
32+
2. Working:
33+
- new Box2(2); // Integer (autoboxed to Number)
34+
- new Box2(3.14); // Double
35+
- Constructor accept karta hai dono, kyunki dono `Number` ke subclasses hain.
36+
- Internally `this.value` me as a `Number` store hota hai.
37+
38+
3. Difference from Generic Class:
39+
- Agar class generic hoti (`class Box<T extends Number>`), toh value specific type me store hoti.
40+
- Yaha class non-generic hai, isliye har value ko `Number` type me store kiya jaata hai.
41+
42+
4. Advantage:
43+
- Reusability → ek hi class ka constructor alag-alag numeric types accept kar leta hai.
44+
- Compile-time safety → sirf `Number` subclasses hi pass kar sakte ho.
45+
- Flexibility → bina class ko generic banaye, tum generic behavior pa lete ho.
46+
47+
✔ Class non-generic hai, lekin constructor generic hai.
48+
✔ `<T extends Number>` ensure karta hai ki sirf numeric types pass ho.
49+
✔ Internally `Number` type ke variable me store hota hai.
50+
✔ Useful jab tumhe sirf `Number` values store karni ho, lekin flexibility maintains karni ho.
51+
*/

0 commit comments

Comments
 (0)