forked from tugul/CoreJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericInterface.java
More file actions
32 lines (26 loc) · 841 Bytes
/
Copy pathGenericInterface.java
File metadata and controls
32 lines (26 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package generics;
/**
* Generic interface
* 3 ways to implement generic interface:
* 1. Concrete class provides specific type
* 2. Concrete class continues using generic type
* 3. Concrete class doesn't use generics at all
* Last one is old way of coding and Java 8 generates compiler warning about this raw type
*/
class Box {}
interface IPackable<T> {
void pack(T t);
}
// 1. provides specific type
class BoxPacker implements IPackable<Box> {
public void pack(Box box){}
}
// 2. continues using generic type
class Packer<T> implements IPackable<T> {
public void pack(T t){}
}
// 3. not using generics triggers raw type warning
class OldPacker implements IPackable {
public void pack(Object o) {} // Overriding method, T must be type Object in this case
public void pack(Box b) {} // Overloading method
}