-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWildCards.java
More file actions
82 lines (66 loc) · 1.67 KB
/
WildCards.java
File metadata and controls
82 lines (66 loc) · 1.67 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Scanner;
class X
{
}
class A extends X implements Serializable
{
}
class B extends A{
}
class C extends A
{
}
class D
{
}
class T
{
//static void print(ArrayList<Object> aList){
static void print(ArrayList<?> aList){
//static void print(ArrayList<? super A> aList){
//static void print(ArrayList<? extends Serializable> aList){
//static void print(ArrayList<? extends A> aList){
//static void print(ArrayList<A> aList){
System.out.println(aList);
}
}
public class WildCards {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Limitation of Generics
// Before Java 5
ArrayList list = new ArrayList(); // Non-Generic
list.add("Hello");
list.add(1000.20);
list.add(true);
list.add(new Scanner(System.in));
String temp = (String)list.get(0);
// Non-Generic can Take anything
// The Problem you need to typecast every time to reterive
// the values
// It treat everything as Object
// Java 5 Generic Collection
ArrayList<String> strList = new ArrayList<>(); //Generic
// Now it can take only String
strList.add("Hello");
//strList.add(1000.20);
//strList.add(true);
//strList.add(new Scanner(System.in));
temp = strList.get(0);
A obj = new B(); // Upcasting
A obj2[] = {new B(), new C(), new A()};
ArrayList<A> aList = new ArrayList<>();
ArrayList<B> bList = new ArrayList<>();
ArrayList<C> cList = new ArrayList<>();
ArrayList<D> dList = new ArrayList<>();
ArrayList<X> xList = new ArrayList<>();
T.print(aList);
T.print(xList);
T.print(bList);
T.print(cList);
T.print(dList);
T.print(strList);
}
}