-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStormyInning.java
More file actions
97 lines (77 loc) · 2.39 KB
/
StormyInning.java
File metadata and controls
97 lines (77 loc) · 2.39 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
97
package exceptions;
/**
* RUN:
* javac exceptions/StormyInning.java && java exceptions.StormyInning
* OUTPUT:
*
*/
// import java.util.logging.*;
// import java.io.*;
public class StormyInning extends Inning implements Storm {
public StormyInning() throws RainedOut, BaseballException {}
public StormyInning(String s) throws Foul, BaseballException {}
// Error: walk() in StormyInning cannot override walk() in Inning
//
// 1) attempting to assign weaker access privileges; was public
// 2) overridden method does not throw PopFoul
//
// void walk() throws PopFoul {}
// Error: event() in StormyInning cannot override event() in Inning
//
// 1) overridden method does not throw RainedOut
//
// public void event() throws RainedOut {}
public void rainHard() throws RainedOut {}
// without "throws BaseballException"
public void event() {}
// atBat() in Inning class throws Strike, Foul !!!
public void atBat() throws PopFoul {}
public static void main(String[] args)
{
try {
StormyInning si = new StormyInning();
si.atBat();
}
catch(PopFoul e) {
System.out.println("Pop foul");
}
catch(RainedOut e) {
System.out.println("Rained out");
}
catch(BaseballException e) {
System.out.println("Common exception");
}
try {
Inning i = new StormyInning();
i.atBat();
}
catch(Strike e) {
System.out.println("Strike");
}
catch(Foul e) {
System.out.println("Foul");
}
catch(RainedOut e) {
System.out.println("Rained out");
}
catch(BaseballException e) {
System.out.println("Common exception");
}
}
}
class BaseballException extends Exception {}
class Foul extends BaseballException {}
class Strike extends BaseballException {}
abstract class Inning {
public Inning() throws BaseballException {}
public void event() throws BaseballException {}
public abstract void atBat() throws Strike, Foul;
public void walk() {}
}
class StormException extends Exception {}
class RainedOut extends StormException {}
class PopFoul extends Foul {}
interface Storm {
public void event() throws RainedOut;
public void rainHard() throws RainedOut;
}