HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🧚
[1기]최종 프로젝트 데브코스
/
📜
[팀13] 사각사각 ✏️
/
🎊
기술 문서
/
💰
sealed
💰

sealed

 

Sealed Classes

  • Sealed로 정의된 클래스나 인터페이스는 다른 클래스들이 상속하거나 구현하는 것을 제한 할 수 있다.
  • 서브 클래스로 사용하기에 아주 좋다!
  • 메인 모델링 및 라이브러리 보안 강화에 유용하다.
    • 모델링 가능성이란? 클래스 계층 구조의 다른 목적은 도메인에 존재하는 다양한 가능성을 모델링하는것!
    • 서브(하위) 클래스에 대한 확장을 제한 함으로서 모델링의 보안을 강화한다고 할 수 있다.

sealed Interface

  • class의 경우와 유사하게 interface에 sealed modifier를 사용하여 interface를 sealed 처리한다.
  • superinterface를 지정하기 위한 extends 절 뒤에 implement class와 subinterface가 permits 절로 지정된다.
  • Java 15의 또 다른 record와 함께 잘 동작한다.
  • record는 암시적으로 final이므로 record가 있는 sealed 계층을 간결하게 표현할 수 있다.
    • 간단하게 새로운 데이터 클래스 생성이 가능
    • value의 단순한 집계를 표현하는 객체 지향 구조
    • programmer가 확장 가능한 동작이 아닌 불변 데이터를 모델링하는데 집중할 수 있도록 도움
    • equals와 accessor 같은 data 기반 method를 자동으로 구현
    • 기존 방식과 migration 호환성 유지

permits

  • 허용되는 타입들만 상속이(implements, extends) 가능하다
  • 허용하는 하위 타입 목록 지정
  • 하위 타입이 존재해야 함
  • record 도 가능하다.
 
// Response와 SeriesListObject 타입만 [interface SeriesSubscribeList] 를 상속받을 수 있다. public sealed interface SeriesSubscribeList permits Response, SeriesListObject { @Schema(name = "SeriesSubscribeList.Response") record Response( List<SeriesListObject> seriesList ) implements SeriesSubscribeList { @Builder //기본 생성자의 Builder를 설정하여 사용할 수 있다. public Response { } } ... @Schema(name = "SeriesSubScribeList.SeriesListObject") record SeriesListObject( Boolean isLiked, Long userId, Long writerId, Long seriesId, String nickname, String thumbnail, String title, String introduceSentence, LocalDate seriesStartDate, LocalDate seriesEndDate, String subscribeStatus, LocalDate subscribeStartDate, LocalDate subscribeEndDate, int likes, Series.Category category ) implements SeriesSubscribeList { @Builder public SeriesListObject { } } ...
 

참고 자료

  • ADT(algebraic data types)도 사용해 볼 것
www.baeldung.com
https://www.baeldung.com/java-sealed-classes-interfaces
JDK 15 New Features
JDK의 버전별 변경 사항은 이곳 을 참고하세요. Java SE 15 Platform JSR 390에 정의된 바와 같이 JSR 390 구현이 목표 실제 Spec은 Final Release Specification 문서를 참고해야 함 전체 JEP Feature 목록은 OpenJDK의 JDK15 문서로 확인할 수 있다.
JDK 15 New Features
https://luvstudy.tistory.com/125
JDK 15 New Features
Interesting features in Java 17
A month ago, Java 17 was released. The new LTS version comes with a lot of performance improvements, new features, and API enhancements. Here are my top features in this release. Sealed Classes or Interfaces Pattern Matching in switch Vector API Foreign Function and Memory API With Java 17, Sealed classes or interfaces are finally out of preview.
Interesting features in Java 17
https://sendilkumarn.com/blog/java-17/
Interesting features in Java 17
Algebraic Data Types - Intro for java devs
If you've ever worked on a java project that involved a database, odds are you used an ORM like Hibernate or JPA. Despite the promise that these systems solve the impedance mismatch problem between the two-dimensional structure of database tables to the richer object graphs of a domain model, it's likely your mapped domain objects pretty closely mirror the structure of the database.
Algebraic Data Types - Intro for java devs
https://www.cargurus.dev/Algebraic-Data-Types-for-java-devs/
Algebraic Data Types - Intro for java devs
 
 
FList<T>를 인터페이스를 Cons 와 Empty만 implements나 extends 할 수 있다.
 
notion image
notion image
notion image
자바 17 특징 : sealed타입
자바 17에 추가된 sealed 타입과 미리보기로 추가된 스위치에서의 패턴 매칭에 대해 알아봅니다.자바 9-16의 주요 특징은 https://youtu.be/7SlDdzVk6GE 영상을 참고하세요.
자바 17 특징 : sealed타입
https://www.youtube.com/watch?v=GJB-RyHKHjY
자바 17 특징 : sealed타입