Skip to content

Commit 3659898

Browse files
committed
refactor(generic-types): replace Object-based Box with type-safe generic BoxIMP<T>
WHAT: - Introduced `BoxIMP<T>` class using generics instead of raw `Object`. - Getter and setter methods now operate directly on the type parameter `T`. - Updated `Main` to demonstrate safe usage with `BoxIMP<Integer>`. WHY: - The earlier `Box` implementation required casting (`(String) box.getValue()`) which caused runtime `ClassCastException`. - Generics enforce compile-time type safety, eliminating the need for unsafe casts. - Improves readability, maintainability, and robustness of the code. HOW: - Defined class as `public class BoxIMP<T>`. - `setValue(T value)` ensures only correct type can be stored. - `getValue()` returns `T` directly, avoiding explicit casting. - Example: `BoxIMP<Integer>` ensures only integers can be set and retrieved safely. BENEFITS: - Prevents runtime casting errors by shifting checks to compile time. - Cleaner, more expressive code (`BoxIMP<Integer>` vs `Box` with Object). - Reusable for multiple types (e.g., `BoxIMP<String>`, `BoxIMP<Double>`). REAL-WORLD APPLICATION: - Similar to how Java Collections Framework uses generics (e.g., `List<String>`). - Useful in designing APIs or libraries where container classes must be flexible but type-safe. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 71844de commit 3659898

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package GenericTypes;
22

33
public class BoxIMP<T> {
4-
/* one or more type parameters
5-
These type parameters are placeholders that are replaced with specific types when the class is instantiated.
6-
*/
74
private T value;
85

96
public T getValue() {
@@ -18,18 +15,27 @@ public void setValue(T value) {
1815
class Main {
1916
public static void main(String[] args) {
2017
BoxIMP<Integer> box = new BoxIMP<>(); // Box is now type-safe
18+
2119
box.setValue(1); // No issue, it's an Integer
20+
2221
Integer i = box.getValue(); // No casting needed
2322
System.out.println(i);
2423
}
2524
}
2625

2726
/*
28-
Here, Box<T> is a generic class. The type parameter T will be replaced with a specific type when an object of Box is created.
29-
Now, the Box class is type-safe, and we will not encounter the ClassCastException because the types are enforced at compile time.
27+
one or more type parameters, These type parameters are placeholders that are replaced with specific types when the
28+
class is instantiated.
29+
30+
Here, Box<T> is a generic class. The type parameter T will be replaced with a specific type when an object of Box is
31+
created.
32+
33+
Now, the Box class is type-safe, and we will not encounter the ClassCastException because the types are enforced at
34+
compile time.
3035
31-
So, In simpler terms, generics allow you to write code that can work with any object type while ensuring type safety at compile time.
36+
So, In simpler terms, generics allow you to write code that can work with any object type while ensuring type safety at
37+
compile time.
3238
3339
Generics help us write more flexible and reusable code. For example, without generics,
3440
we would have to write multiple versions of the same class to handle different data types, leading to code duplication.
35-
*/
41+
*/

0 commit comments

Comments
 (0)