Skip to content

Commit

Permalink
JEP 409: Sealed Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimatay committed Nov 21, 2023
1 parent 9ba0171 commit 33a6073
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This repository contains Java examples that are designed to track and document t
* [JEP 431](https://openjdk.org/jeps/431): Sequenced Collections
* [JEP 444](https://openjdk.org/jeps/444): Virtual Threads

* [Java 17](java-17/) (September, 2021)
* [JEP 409](https://openjdk.org/jeps/409): Sealed Classes

* [Java 16](java-16/) (March, 2021)
* [JEP 395](https://openjdk.java.net/jeps/395): Records

Expand Down
20 changes: 20 additions & 0 deletions java-17/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.ibrahimatay</groupId>
<artifactId>Java-Features</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>java-17</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
54 changes: 54 additions & 0 deletions java-17/src/main/java/com/ibrahimatay/JEP409SealedClasses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ibrahimatay;

/*
JEP 409: Sealed Classes
https://openjdk.org/jeps/409
*/

import java.util.UUID;

public class JEP409SealedClasses {
public static void main(String[] args) {
System.out.println(getNumberOfSeats(new Truck(100, UUID.randomUUID().toString())));
System.out.println(getNumberOfSeats(new Car(20, UUID.randomUUID().toString())));
}

static int getNumberOfSeats(Vehicle vehicle) {
if (vehicle instanceof Car) {
return ((Car) vehicle).getNumberOfSeats();
} else if (vehicle instanceof Truck) {
return ((Truck) vehicle).getLoadCapacity();
} else {
throw new RuntimeException("Unknown instance of Vehicle");
}
}
}

sealed interface Vehicle permits Car, Truck {
String getRegistrationNumber();
}

record Car(int numberOfSeats, String registrationNumber) implements Vehicle {

@Override
public String getRegistrationNumber() {
return registrationNumber;
}

public int getNumberOfSeats() {
return numberOfSeats;
}
}

record Truck(int loadCapacity, String registrationNumber) implements Vehicle {

@Override
public String getRegistrationNumber() {
return registrationNumber;
}

public int getLoadCapacity() {
return loadCapacity;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>java-6</module>
<module>java-16</module>
<module>java-21</module>
<module>java-17</module>
</modules>

<properties>
Expand Down

0 comments on commit 33a6073

Please sign in to comment.