Java 15 Tutorial
Java Other Versions Tutorials
Java 15 Useful Resources
Selected Reading
- Java 15 - Deprecation & Removals
- Java 15 - Other Enhancements
- Java 15 - Deprecation & Removals
- Java 15 - Other Changes
- Java 15 - Garbage Collectors
- Java 15 - Hidden Classes
- Java 15 - record & Sealed Classes
- Java 15 - record
- Java 15 - Text Blocks
- Java 15 - Pattern for instanceOf
- Java 15 - Sealed Classes
- Java 15 - Environment Setup
- Java 15 - Overview
- Java 15 - Home
Java Other Versions Tutorials
- Java 14 Tutorial
- Java 13 Tutorial
- Java 12 Tutorial
- Java 11 Tutorial
- Java 10 Tutorial
- Java 9 Tutorial
- Java 8 Tutorial
- Java Tutorial
Java 15 Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java 15 - record & Sealed Classes
Java 15 - Record for Sealed Interfaces
As records are final by default and can extend interfaces. We can define sealed interfaces and let record implement them for better code management.
Example
Consider the following example −
ApiTester.java
pubpc class APITester { pubpc static void main(String[] args) { Person employee = new Employee(23, "Robert"); System.out.println(employee.id()); System.out.println(employee.name()); } } sealed interface Person permits Employee, Manager { int id(); String name(); } record Employee(int id, String name) implements Person {} record Manager(int id, String name) implements Person {}
Compile and Run the program
$javac -Xpnt:preview --enable-preview -source 15 APITester.java $java --enable-preview APITester
Output
23 RobertAdvertisements