forked from patniemeyer/learningjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.java
More file actions
96 lines (72 loc) · 1.65 KB
/
Copy pathB.java
File metadata and controls
96 lines (72 loc) · 1.65 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.*;
/*
When subclassing in Java the best we
*/
/*
Start with extending an arbitrary type
go through subclassing exercise
class B <E extends Date>
{
void take( E element ) { }
}
// this is ok, but unchecked warnings on all methods
//class C extends B { }
// when extending must limit the bounds again, else compiler warning that E is
// not within its bounds
class C<E extends Date> extends B<E> { }
class Main {
public static void main( String[] args )
{
System.out.println("main");
// unchecked warning
// new C().take( new Date() );
new C<Date>().take( new Date() );
// a B<Date> is a B
System.out.println( new B<Date>() instanceof B ); // true
System.out.println( new B<Date>() instanceof B<Date> ); // error
System.out.println("done");
}
}
*/
/*
Now have it extend itself
class B <E extends B>
{
void take( E element ) { }
}
class C<E extends B> extends B<E> { }
class Main {
public static void main( String[] args )
{
System.out.println("main");
// plain extends B all ok
new B<B>();
// here's the problem, any B works
// A B<anything> is a still a B
new B<B>().take( new B() );
new B<B>().take( new B<B>() );
new C<B>().take( new C<B>() );
new C<B>().take( new B<B>() );
new C<C>().take( new C<C>() );
System.out.println("done");
}
}
*/
/*
Now try to limit param type to a specific paramaterization of B
*/
class B <E extends B<E>>
{
void take( E element ) { }
}
class C<E extends C<E>> extends B<E> { }
class Main {
public static void main( String[] args )
{
System.out.println("main");
//new C<C>(); // no
//new C<B>();
//new B<B>();
System.out.println("done");
}
}