Making Use of Sealed Classes in Java

Sdílet
Vložit
  • čas přidán 1. 08. 2024
  • In this session, you’ll see the benefits of using sealed classes, understand how to implement them, and learn how this feature interplays with other powerful features in Java.
    An Oracle Developer Live - Java Innovation Session presented by Dr. Venkat Subramaniam (Agile Developer, Inc. founder) ➱ agiledeveloper.com/
    ⎯⎯⎯⎯⎯⎯ Chapters ⎯⎯⎯⎯⎯⎯
    00:00 Introduction
    00:30 What are Sealed classes.
    05:28 How Sealed classes work?
    23:14 Sealed Classes benefits
    Categories: #Java #Java17 #Development
  • Věda a technologie

Komentáře • 26

  • @saidooubella
    @saidooubella Před 2 lety +30

    What is your favorite presentations software?
    Everyone: MS PowerPoint, Google Slides, etc.
    Dr. Venkat: Vim, take it or leave it.
    😄😄😄😄

    • @modraccin9514
      @modraccin9514 Před 2 lety +2

      I love it. Text based formats rule. Everyone can read and work with them. *forever!* And they don't endanger your computer.

  • @Vanuatoo
    @Vanuatoo Před 2 lety +15

    I wish you were my teacher 25 years ago in uni

  • @wwhill8033
    @wwhill8033 Před 2 lety +11

    Excellent explanation Venkat, thanks

    • @java
      @java  Před 2 lety +2

      Glad you liked it

  • @hemaldaha
    @hemaldaha Před 2 lety

    This would be an interesting mechanism in multiple development team env. Senior architect can define a certain set of classes to be must extended but extended only once. Plus you can permit who can extend. Utilizing this in designs would be something to keep in mind.

  • @jay_sensz
    @jay_sensz Před 2 lety +4

    14:30 I think the main issue is that you're trying to access RedLight (which is package-protected in the package `tl`) from the default package.

  • @sovrinfo
    @sovrinfo Před 2 lety

    Thanks you a lot

  • @devoiddude
    @devoiddude Před 2 lety

    what editor are you using ?

  • @dev-skills
    @dev-skills Před 2 lety +1

    23:19 Initially was wondering how could be dated 01/19/2009, later realized that's from the background wallpaper.

  • @DevMultitask
    @DevMultitask Před rokem +2

    Great video, and my opinion is sealed 😅

  • @antoniovieiradasilvajunior666

    am i wrong in think that sealed models can be a way to get rid of unchecked cast in generics?

  • @user80204
    @user80204 Před 2 lety

    from where Sealed come from?!

  • @chitthiaayeehai
    @chitthiaayeehai Před 2 lety +2

    Let's suppose Java Seals List Interface only for Lists defined by java then i will not be able to extend List to create my own definition rt ?
    Open for extension and closed for modification => now Sealed for modification.
    Hopping all these are happening for good and for reducing the runtime / unexpected errors we get.

    • @_SG_1
      @_SG_1 Před 2 lety +4

      Yes, it's a new form of "final". There's an escape hatch by declaring a sub-class "non-sealed". I hope it becomes best-practice to add one by library devs. Otherwise we end up in the same situation as language ecosystems where final is the default, where every other PR is asking to lift it or provide a hook.

    • @SourabhBhat
      @SourabhBhat Před 2 lety +2

      I do not think that old libraries will seal existing public interfaces / classes, that will be a breaking change. Even with new code, we must only seal well defined closed class hierarchy.
      Two questions to ask before deciding to seal a class is "Do we use instanceof for deciding the behavior of our programs?" and "Does a default behavior make sense?" If the answer is Yes to first question and No to the second, only then we can think of sealing the class hierarchy. The compiler will ensure the correctness of our code automatically.

    • @_SG_1
      @_SG_1 Před 2 lety +1

      @@SourabhBhat What's often overlooked is that a closed class hierarchy within the library might make sense at the point of creation, but later on a user might want to override or extend this functionality in way not predicted by the library owner. Leading to discussions where the owner often takes a defensive stance "you're not using it as intended". Prime example within the Java ecosystem is JavaFX where so much is final that it gets really hard to customize anything (e.g. table layout).
      In essence sealed gives us two things: 1) library owners can thwart annoying support requests of the form "I tried to write my own implementation" and 2) you can safe the default in switch-case instanceof pattern matching and get exhaustiveness errors. 1) is questionable and 2) is aswell if you like me regard instanceof as code-smell / poor-mans polymorphism.

    • @bentels5340
      @bentels5340 Před 2 lety +1

      I think Venkat covered that at the beginning. Sealed hierarchies only make sense to model things for which there really are only a finite number of variations. So you might create a sealed hierarchy of stellar body types (Star, Planet, Satellite -> Moon, Asteroid), but not List.

    • @SourabhBhat
      @SourabhBhat Před 2 lety

      ​@@_SG_1 Sealing of interfaces/classes must be done very carefully indeed. Sealing any public interface defeats the whole purpose of an interface. Similarly, OOP heavily uses overriding of methods. However, there are a few cases where the logic changes based on an instanceof check. In such a situation the library developer has to throw an exception at runtime for any user defined class. This situation can be avoided by using a sealed class. I think this is where sealed class / interface can be used.
      In a code maintained by me, it makes sense to seal only a couple of internal interfaces. Nevertheless, it will make it very robust while extending the code. In one situation, I had to simulate a sealed class by defining an enum and an interface with one of the methods returning the object from the enum. I can get rid of the enum (and the method from interface) now.

  • @elendal
    @elendal Před 2 lety +3

    explain to me like i'm 5, what problem does it solve? We can't foresee all possible scenarios, so why close the hierarchy for modifications? Also, it is not really sealed if it can be unsealed, so what's the point?

    • @elendal
      @elendal Před 2 lety +2

      looks like it will be used by people who are control freaks and we will have to deal with this when working with old libraries that use this.

    • @SourabhBhat
      @SourabhBhat Před 2 lety +14

      In short, a "sealed class" is like an enum with benefits of a class.
      Consider you have written a library with an interface called Geometry having one abstract method area(), which is implemented by classes Square and Triangle within your library.
      The users of your library can pass either Square or Triangle object to a List for processing by your library. How can you prevent a user from passing an instance of say "class Tree implements Geometry" with their own implementation? How can you be sure of the contents of the list (that it contains only valid Geometry objects) when processing it within your library? Probably you will need to throw a runtime exception. If you use sealed classes this will be ensured automatically.
      The second use case will be in the future version of Java. Say that the Geometry interface is used at 10 different places in your code (and possibly more places by users of your library). If you decide to add a new class called Circle implementing Geometry in your library, how do you ensure that Circle is not being treated as a default case in one of those 10 places (and your library users)? The compiler cannot help you if it does not know about your class hierarchy. In the future, as Dr. Venkat said in this video, Sealed class will allow to switch over pre-defined sealed hierarchy thus the compiler will show error at those 10 places in your code during compile time. This will work even if a class is non-sealed.
      I think that sealed classes is a misunderstood feature but it will help both library developers and library users alike to ensure correctness of programs. This will make Java programs more robust and trustworthy.

    • @DeepakLalchandaniProfile
      @DeepakLalchandaniProfile Před 2 lety

      @@SourabhBhat Can you demonstrate this with a Java Code so that I can execute in IDE. Can you demonstrate both the points .do share the Java code on a blog / github

    • @francoisloriot2674
      @francoisloriot2674 Před 2 lety

      you could say the same think for final. and there will be plenty of cases where you can foresee all possible scenarios at design time and we will avoid suprises with sealed classes.

  • @DinHamburg
    @DinHamburg Před 2 lety +1

    This feature doesn't make much sense by itself. The purpose is more to support the new pattern matching stuff.