-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise1.java
69 lines (61 loc) · 1.75 KB
/
Exercise1.java
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
/**
* @author viktorvoltz
*/
interface HasBatteries {
}
interface Waterproof {
}
interface Shoots {
}
class Toy {
// Comment out the following default constructor
// to see NoSuchMethodError from (*1*)
//Toy() {
//}
Toy(int i) {
}
}
class FancyToy extends Toy
implements HasBatteries, Waterproof, Shoots {
FancyToy() {
super(1);
}
}
class ToyTest {
static void printInfo(Class<?> cc) {
System.out.println("Class name: " + cc.getName() +
" is interface? [" + cc.isInterface() + "]");
System.out.println("Simple name: " + cc.getSimpleName());
System.out.println("Canonical name : " + cc.getCanonicalName());
}
public static void main(String[] args) {
Class<?> c = null;
try {
c = Class.forName("FancyToy");
} catch (ClassNotFoundException e) {
System.out.println("Can’t find FancyToy");
System.exit(1);
}
printInfo(c);
for (Class<?> face : c.getInterfaces())
printInfo(face);
Class<?> up = c.getSuperclass();
Object obj = null;
try {
// Requires default constructor:
//cannot instantiate
obj = up.newInstance();
//System.out.println("obj " + obj + " " +obj.getClass().getName());
} catch (InstantiationException e) {
System.out.println("Cannot instantiate");
System.exit(1);
} catch (IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
} catch (NoSuchMethodError e){
System.out.println("NoSuchMethodError");
System.exit(1);
}
printInfo(obj.getClass());
}
}