Skip to content

Commit 0cdd340

Browse files
java generics
1 parent 06bd86e commit 0cdd340

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sporadic.java_generics;
2+
3+
/**
4+
* This is a generic class that operates on objects of only T
5+
* type.
6+
* @param <T>
7+
* the type of the value
8+
*/
9+
public class GenericClass<T> extends ParentClass {
10+
// T stands for "Type"
11+
private T t;
12+
13+
public void set(T t) {
14+
this.t = t;
15+
}
16+
17+
public T get() {
18+
return t;
19+
}
20+
21+
public static void main(String[] args) {
22+
GenericClass<Integer> integerBox = new GenericClass<Integer>();
23+
GenericClass<String> stringBox = new GenericClass<String>();
24+
25+
integerBox.add(new Integer(10));
26+
stringBox.add(new String("Hello World"));
27+
28+
System.out.printf("Integer Value :%d\n\n", integerBox.get());
29+
System.out.printf("String Value :%s\n", stringBox.get());
30+
}
31+
32+
private void add(T t) {
33+
this.t = t;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package sporadic.java_generics;
2+
3+
/**
4+
* This is a non-generic Box class that operates on objects of any type. */
5+
public class NonGenericClass {
6+
private Object object;
7+
8+
public void set(Object object) { this.object = object; }
9+
public Object get() { return object; }
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package sporadic.java_generics;
2+
3+
public class ParentClass {
4+
5+
@Override
6+
public String toString() {
7+
return "This class is serving as the parent class for other classes to extend to demo java genric types for my own understanding.\nIt's calling the parent class: SteveSunParentClass [getClass()=" + getClass()
8+
+ ", hashCode()=" + hashCode() + ", toString()="
9+
+ super.toString() + "]";
10+
}
11+
12+
}

0 commit comments

Comments
 (0)