Skip to content

Commit d2d4062

Browse files
committed
Java 15: sealed type
1 parent 567696d commit d2d4062

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

java-15/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ To run each example use: `java --enable-preview --source 15 <FileName.java>`
77
### Language
88

99
* Sealed types
10+
* Restrict which types can extend/implement a type
11+
* Constraints:
12+
* The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, the same package.
13+
* Every permitted subclass must directly extend the sealed class.
14+
* Every permitted subclass must choose a modifier to describe how it continues the sealing initiated by its superclass:
15+
* `final`
16+
* `sealed`
17+
* `non-sealed` (back to a normal class open to extensibility)
1018
* Pattern matching for `instanceof` (second preview)
1119
* Text blocks (standard)
1220
* Records (second preview)

java-15/SealedTypesExample.java

+36-19
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,58 @@ public static void main(String[] args) {
77
var player = new Player();
88
var entity = player.getCollidingEntity();
99

10-
switch (entity) {
11-
case Player:
12-
player.move();
13-
entity.move();
14-
break;
15-
case Enemy:
16-
player.die();
17-
break;
18-
case Object:
19-
object.move();
20-
break;
21-
// we dont need to do this (nor ifs) - because we restricted the implementations types (we know them)
22-
// default:
23-
// throw IllegalStateException("Unexpected entity");
10+
if (entity instanceof Player p) {
11+
player.move();
12+
p.move();
13+
} else if (entity instanceof Enemy e) {
14+
player.kill(e);
15+
} else if (entity instanceof SceneryObject o) {
16+
o.move();
2417
}
18+
// we dont need to do this (nor ifs) - because we restricted the implementations types (we know them)
19+
// else {
20+
// throw IllegalStateException("Unexpected entity");
21+
// }
2522
}
2623
}
2724

2825
/**
2926
* Sealed types will help us to restrict which types can extend/implement a type.
3027
* Will allow us to write less verbose polymorphic code.
28+
*
29+
* Constraints:
30+
* - The sealed class and its permitted subclasses must belong to the same module, and, if declared in an unnamed module, the same package.
31+
* - Every permitted subclass must directly extend the sealed class.
32+
* - Every permitted subclass must choose a modifier to describe how it continues the sealing initiated by its superclass:
33+
* - `final`
34+
* - `sealed`
35+
* - `non-sealed` (back to a normal class open to extensibility)
3136
*/
32-
sealed abstract class GameEntity
33-
permits Player, Enemy, Object {}
37+
sealed abstract class GameEntity permits Player, Enemy, SceneryObject {
38+
public void move() {
39+
System.out.println("Moving");
40+
}
41+
42+
public void destroy() {
43+
System.out.println("Destroying");
44+
}
45+
}
3446

35-
class Player extends GameEntity {
47+
final class Player extends GameEntity {
3648
/**
3749
* Returns the entity which is colling with the player.
3850
*/
3951
public GameEntity getCollidingEntity() {
4052
// obscure logic
4153
return new Enemy();
4254
}
55+
56+
public void kill(GameEntity killedBy) {
57+
// obscure death
58+
System.out.println("Killing");
59+
}
4360
}
4461

45-
class Enemy extends GameEntity {}
62+
final class Enemy extends GameEntity {}
4663

47-
class Object extends GameEntity {}
64+
non-sealed class SceneryObject extends GameEntity {}

0 commit comments

Comments
 (0)