Skip to content

Commit 9c215df

Browse files
committed
feat(generic-interface): add example of bounded type parameters with class and interface constraints
WHAT: - Introduced `Printable` interface with a `print()` method. - Implemented `MyNumber` class extending `Number` and implementing `Printable`. - Created a generic class `Box<T extends Number & Printable>`: - Restricts type parameter `T` to extend `Number` and also implement `Printable`. - Demonstrates that class must come first, followed by interfaces when declaring multiple bounds. - Added `Test` class to show usage of `Box<MyNumber>`. WHY: - Demonstrates how to enforce multiple constraints on generic type parameters in Java. - Shows real-world relevance where a type must satisfy both numeric behavior (via `Number`) and custom contract (via `Printable`). DETAILS: - `MyNumber` overrides `Number` methods (`intValue`, `longValue`, `floatValue`, `doubleValue`) and adds a custom print. - `Box<T>` stores a bounded type, ensures type safety, and allows invoking both `Number` methods and `Printable` methods. - In `main()`, a `Box<MyNumber>` object is created and `display()` calls the `print()` method. KEY LEARNING: - Multiple bounds in generics are declared using `&`. - Rule: A class must appear first, followed by any number of interfaces. - This ensures strong type safety while allowing rich polymorphism. BENEFITS: - Enforces contracts at compile-time rather than relying on runtime checks. - Makes code reusable, type-safe, and flexible. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 01dc531 commit 9c215df

File tree

1 file changed

+94
-0
lines changed
  • Section24JavaGenerics/src/GenericInterface

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package GenericInterface;
2+
3+
interface Printable {
4+
void print();
5+
}
6+
7+
class MyNumber extends Number implements Printable {
8+
private final int value;
9+
10+
public MyNumber(int value) {
11+
this.value = value;
12+
}
13+
14+
public void print() {
15+
System.out.println("MyNumber: "+value);
16+
}
17+
18+
public int intValue() {
19+
return value;
20+
}
21+
22+
public long longValue() {
23+
return value;
24+
}
25+
26+
public float floatValue() {
27+
return value;
28+
}
29+
30+
public double doubleValue() {
31+
return 0;
32+
}
33+
}
34+
35+
class Box<T extends Number & Printable> {
36+
//First class and then Interface after the "&".
37+
public T item;
38+
39+
public Box(T item) {
40+
this.item = item;
41+
}
42+
43+
public void display() {
44+
item.print();
45+
}
46+
47+
public T getItem() {
48+
return item;
49+
}
50+
}
51+
52+
public class Test {
53+
public static void main(String[] args) {
54+
MyNumber myNumber = new MyNumber(12);
55+
Box<MyNumber> box = new Box<>(myNumber);
56+
57+
box.display();
58+
}
59+
}
60+
61+
62+
/*
63+
✔ Purpose:
64+
- `Box<T extends Number & Printable>`: T sirf aise types ho sakte hain jo
65+
(1) `Number` extend karte hain (numeric behavior) AND
66+
(2) `Printable` implement karte hain (print() method).
67+
- Isse compile-time pe dono guarantees milti hain: numeric methods + print capability.
68+
69+
✔ Order of bounds:
70+
- Java rule: agar a class bound ho to pehle woh class likho, phir interfaces.
71+
Example: `T extends Number & Printable` // correct
72+
(Class first, then interfaces.)
73+
74+
✔ MyNumber:
75+
- `MyNumber` extends `Number` and implements `Printable`, isliye `Box<MyNumber>` valid hai.
76+
- It implements `intValue(), longValue(), floatValue(), doubleValue()` as required by `Number`.
77+
- Note / Warning: `doubleValue()` currently returns `0` — that looks like a bug.
78+
It should return the numeric value (e.g. `return value;`) to preserve correctness.
79+
80+
✔ Box behavior:
81+
- `Box` stores a `T item` and `display()` simply delegates to `item.print()`.
82+
- This is a neat example of combining numeric computation capability (Number) with a behavior interface (Printable).
83+
84+
✔ When to use this pattern:
85+
- Use when you need both structural guarantees: numeric operations (Number API) and a custom behavior (like print/log).
86+
- It keeps the API type-safe and expressive: callers know they can both treat the item as a number AND ask it to
87+
print itself.
88+
89+
Quick Recap
90+
✔ Bounded generics allow precise compile-time constraints.
91+
✔ Syntax: class bound first, then `&` interfaces.
92+
✔ Implement required `Number` methods correctly (fix `doubleValue()` to return `value`).
93+
✔ Pattern is useful for small DSLs where objects must be both numeric and behaviorful.
94+
*/

0 commit comments

Comments
 (0)