Sealed Classes VS. Enum Classes VS. Sealed Interfaces - When to Use Which?

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 ส.ค. 2024
  • In this video I'll clear the confusion between the different ways in Kotlin to combine related options together.
    ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/...
    Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
    pl-coding.com/...
    Join this channel to get access to perks:
    / @philipplackner
    Join my Discord server:
    / discord
    Regular programming advice on my Instagram page: / _philipplackner_
    Checkout my GitHub: github.com/phi...
    You like my free content? Here you can buy me a coffee:
    www.buymeacoff...

ความคิดเห็น • 110

  • @VilRapt
    @VilRapt 2 ปีที่แล้ว +133

    Sealed interfaces is not about "saving a little bit of code" it is to reduce the memory allocated, which is twice less for the interface in comparison to the class.

    • @deepakbisht4957
      @deepakbisht4957 2 ปีที่แล้ว +1

      Of course Sealed classes come with their own costs that many programmers don't consider...

    • @dekus80
      @dekus80 2 ปีที่แล้ว +2

      And why all use sealed class ever with empty constructor, if as we can see in the video the same with sealed interface (object, data class)? Don't know about ability to use interface?

    • @VilRapt
      @VilRapt 2 ปีที่แล้ว

      @@dekus80 o believe the reasoning explained well in the video. There are case when you simply can't.

    • @ArthurKhazbs
      @ArthurKhazbs ปีที่แล้ว +3

      Also interfaces give the nifty ability for each subtype to implement multiple of them, in contrast to classes only allowing to extend one of them. If for some reason you need the children of your sealed type to extend other classes, you can do so with the sealed interface. And obviously, if the children are interfaces, then their sealed supertype cannot be a class, only an interface.

  • @yossihorvitz5134
    @yossihorvitz5134 2 ปีที่แล้ว +46

    4:30 - press TAB to delete the remaining after auto complete without delete it yourself

    • @AmitJayant
      @AmitJayant 2 ปีที่แล้ว

      Did not work for me. Can u explain?

  • @stoyan_vuchev
    @stoyan_vuchev 2 ปีที่แล้ว +18

    I was always wandering what's the difference between Sealed and Enum classes. I didn't knew that the Enum classes have more functionality than just declaring a constants. Thank you for explaining the differences in detail! 🤗🔥

    • @riyupapa39
      @riyupapa39 2 ปีที่แล้ว +1

      Me too.... I also wandering this difference~

  • @OrlanDroyd
    @OrlanDroyd 6 หลายเดือนก่อน +1

    This type of topic is fascinating and is very useful for job interviews and personal improvement. Keep creating this type of content too ♥

  • @mahamanbachiribrahim9884
    @mahamanbachiribrahim9884 2 ปีที่แล้ว +1

    Thanks Philip, yesterday I searched the difference between sealed class and sealed interface so your video is welcome
    Big up

  • @DeLL116
    @DeLL116 ปีที่แล้ว

    Man... Thank you so much. You are answering all the questions I have for Kotlin and Android. It's like you're reading my mind. Again, thank you.

  • @huseyinbuyukcolak4550
    @huseyinbuyukcolak4550 2 ปีที่แล้ว

    man i tried every other video but this is just legit thanks a lot man

  • @12nileyfan
    @12nileyfan ปีที่แล้ว

    thanks for this content, I agree with you, the Kotlin documentation wasn't that clear about the difference between Sealed interfaces and sealed classes. It is now clear to me :)

  • @raheemadamboev
    @raheemadamboev 2 ปีที่แล้ว +1

    Great video make many videos like this!

  • @devanshu_on_youtube
    @devanshu_on_youtube 2 ปีที่แล้ว +3

    Hey Philipp! Nice video.. just wanted to add a minor point- Kotlin stdlib 1.3 and above allows us to iterate over/perform transformations over subclass instances of a sealed class - though it is a bit more expensive than enums. But the good news is you don't need to keep track of a separate list for this purpose.
    Goes something like this:
    SealedClassExample::class.sealedSubclasses.map { it.objectInstance as SealedClassExample }.forEach { }
    (the casting is to assert that we know a nullable case won't exist)

  • @himanshu7bansal
    @himanshu7bansal 2 ปีที่แล้ว +2

    At this point,
    You are making videos on my recent doubts/confusion,
    Thanks alot, keep it going 💪🏻

  • @l3nvyx70
    @l3nvyx70 2 ปีที่แล้ว

    BROOO thankyou so much, this really helped and the tutorial was really easy to use as well :)

  • @josephofem5448
    @josephofem5448 2 ปีที่แล้ว +4

    Thanks Phil, you have really made my development journey an easy on. However, I will love you to make a video on how to implement mqtt or any form of broadcast receiver with clean architecture. Thanks

  • @AliouneDupuy
    @AliouneDupuy 2 ปีที่แล้ว +5

    Hey 👋. Could you make a video about android notification. More importantly how to use dynamic title and text from a database everytime that the notification fires. Thanks in advance.

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      Thanks for the idea :)

    • @top10channels31
      @top10channels31 2 ปีที่แล้ว

      You can send data payload with notifications and retrieve it in service class as remote messages

  • @moldovanpeter5123
    @moldovanpeter5123 2 ปีที่แล้ว +3

    Nice vid, how about Creating Shortcuts? Static, Dynamic, Pinned, maybe it could help others if they want to shortcut some of their features.

  • @jagat.jeeban
    @jagat.jeeban 2 ปีที่แล้ว

    Thank you for making this video. 🙌

  • @joaovictorsz
    @joaovictorsz ปีที่แล้ว

    Thank you for explaining. I like a lot of your videos.

  • @theophilus494
    @theophilus494 2 ปีที่แล้ว +2

    Thanks Philipp for your tutorials.. it's always like you see some of my struggles on stuffs like this. I want to make a request that can you do a tutorial on workmanager firing a notification with upload progress of a file using retrofit. Thanks 🙏

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +2

      I have a full workmanager guide already, would just require a few adjustments

    • @theophilus494
      @theophilus494 2 ปีที่แล้ว

      @@PhilippLackner Ok great

  • @sahil-nz7vk
    @sahil-nz7vk 2 ปีที่แล้ว +1

    Thanks! I was actually following another tutorial of yours, and when I saw you were using sealed classes instead of enums, I was trying to understand the difference. This really helped clear it up!

  • @baijusharma6027
    @baijusharma6027 2 ปีที่แล้ว +2

    Hi Philipp, You always bring something new to us and clear our confusion.

  • @socheatlay17
    @socheatlay17 2 ปีที่แล้ว

    I have been waiting for this video for soooooooo long

  • @ArthurKhazbs
    @ArthurKhazbs ปีที่แล้ว +1

    I think it's quite remarkable how subtypes of sealed types are almost always nested inside them. It feels like an unspoken rule of sealed types to declare subtypes this way, although nothing stops anybody from declaring them outside.

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว +1

      Because it's much more intuitive to access the values. If not declared inside, how will you know which values belong to the sealed class?

  • @giulianocarmona7250
    @giulianocarmona7250 ปีที่แล้ว

    Massive explanation dude

  • @111shetty
    @111shetty 4 หลายเดือนก่อน

    Hi
    I love watching your videos very easy to understand all difficult topics in android in that way it would be nice to make video on ci/cd integration iniside slack channel

  • @omarredani5580
    @omarredani5580 2 ปีที่แล้ว

    I can suggest to make a video about advanced feature in compose or build a small project using MVU or MVI architecture.

  • @archestro7698
    @archestro7698 2 ปีที่แล้ว

    Thank you.

  • @brandonsgas
    @brandonsgas 2 ปีที่แล้ว

    another way of saying that all a sealed class or enums types are known at compile time is that they are algebraic classes

  • @GB-nn2cx
    @GB-nn2cx ปีที่แล้ว

    Thanks a lot

  • @westiti6650
    @westiti6650 ปีที่แล้ว

    Great explanation bro. Thanks

  • @i.ankitmishra
    @i.ankitmishra 2 ปีที่แล้ว +3

    I like your IDE theme. Please give name of the theme or the plugin name you have used for theme. 🙂 P.S. I am a big fan. Keep making such videos.

  • @viki9126
    @viki9126 2 ปีที่แล้ว

    thanks for a great video

  • @cub3608
    @cub3608 ปีที่แล้ว

    thank you greatly

  • @anibalbojorquez4800
    @anibalbojorquez4800 ปีที่แล้ว

    Great video!

  • @mahendranv6264
    @mahendranv6264 ปีที่แล้ว

    Nice explaintaion bro 👌

  • @ggstb
    @ggstb 2 ปีที่แล้ว

    Your videos helped me get 12k $ job with 3 years of experience. Thanks man

  • @sagargandhi5080
    @sagargandhi5080 2 ปีที่แล้ว +2

    Hi Philipp, can you please make some videos on Unit and UI testing with compose and MVVM.

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +1

      Already have that, search for ultimate testing guide

    • @sagargandhi5080
      @sagargandhi5080 2 ปีที่แล้ว

      @@PhilippLackner aah great, thanks

  • @Adam0001
    @Adam0001 2 ปีที่แล้ว

    Thanks Philipp 👍

  • @behnawm
    @behnawm 2 ปีที่แล้ว +1

    Thank you philipp!
    Can you make a course explaining Paging3 in detail? There aren't many educative stuff about Paging3 (of course except Google's complicated Codelabs!) .

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +3

      I have a video about paging where I also explained why I don't like Paging3 :)

  • @olohialli9289
    @olohialli9289 2 ปีที่แล้ว

    Pls make a video on consuming graph ql apis

  • @yehor_zhyr
    @yehor_zhyr 2 ปีที่แล้ว +1

    Great video, thank you. Yes, sealed interfaces are definitely simpler than they look. I think for the future it would be interesting to see about compose optimization, something about @Stable, @Immutable annotations.🙂

  • @AmitJayant
    @AmitJayant 2 ปีที่แล้ว

    Great video as always. Cleared many doubts.
    btw .. What's the theme and color scheme you using in Android Studio?

  • @MrDorperezz
    @MrDorperezz ปีที่แล้ว

    Thanks champ

  • @muhannddh
    @muhannddh 2 ปีที่แล้ว +1

    Could you please make tutorial video about room encryption

  • @jjhoninatan55sabadi
    @jjhoninatan55sabadi 2 ปีที่แล้ว +1

    Please a video about baseline profilers

  • @xandrviking1113
    @xandrviking1113 ปีที่แล้ว

    Cool video, thanks and like ☺

  • @DouglasRoberto_
    @DouglasRoberto_ ปีที่แล้ว

    What's the name of that color theme you're using?

  • @elangovankr110
    @elangovankr110 2 ปีที่แล้ว

    Can you make video on advanced concepts in Recycleview

  • @gautamsharma9823
    @gautamsharma9823 ปีที่แล้ว

    First of all i will thanks you for the amazing video, i have one question in my mind related to accessibility of sealed class, as you have told that we can not inherit the sealed class in other module but it is also not inheritable inside the other file or packages.can you please make it clear?

  • @ComposeCampus
    @ComposeCampus 7 หลายเดือนก่อน

    8:50 It's true.

  • @ashadzakariya6810
    @ashadzakariya6810 2 ปีที่แล้ว

    I'm too busy reading these comnts and not paying attention again.....and I'm Nice tutorialgh

  • @Guilo583
    @Guilo583 2 ปีที่แล้ว

    Thanks for the video. could you please make video on apoloclient and graphql

  • @artsbrand
    @artsbrand ปีที่แล้ว

    Hey Filipp, could you make tutorial how you can create web app + Kotlin + database + docker + docker compose + some cloud?

  • @TuanBuianonymous
    @TuanBuianonymous ปีที่แล้ว

    Can you make a tutorial about using openCV in android

  • @etelot
    @etelot ปีที่แล้ว

    Are you the guy that StackOverflow uses in a ad?

  • @mdisi5967
    @mdisi5967 2 ปีที่แล้ว

    Thank you !!

  • @freshtechtips
    @freshtechtips 2 ปีที่แล้ว

    What is the name of your ide theme plugin?

  • @mustafaammar551
    @mustafaammar551 2 ปีที่แล้ว

    VERY COOL
    YOU ARE THE BEST 👍👍👍👍

  • @dev_jeongdaeri
    @dev_jeongdaeri 2 ปีที่แล้ว

    Super cool 😎

  • @love_khush
    @love_khush 2 ปีที่แล้ว

    You are really great 👍

  • @ugursener1709
    @ugursener1709 2 ปีที่แล้ว

    Thank you a lot for the video, it is really explict and helpful for beginners.

  • @nagendranm.s.4512
    @nagendranm.s.4512 2 ปีที่แล้ว

    Please teach a ndk, automotive infotainment(aosp), Ble related tutorials

  • @chitye-aung
    @chitye-aung 2 ปีที่แล้ว

    Awesome ❤️❤️

  • @dudziks
    @dudziks 2 ปีที่แล้ว

    Hi Philipp, currently I wonder how to use FirebaseUI with Jetpack Compose. Is it good topic for you to make a video?

  • @nipunshah1373
    @nipunshah1373 2 ปีที่แล้ว

    Can you consider explaining concept about RxKotlin or RxJava

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว

      Got tons of videos about flows. All you need

  • @kachu_sha
    @kachu_sha 2 ปีที่แล้ว

    Can there be “too many” sealed classes in a project? 🤔

  • @achalbadgujar9519
    @achalbadgujar9519 2 ปีที่แล้ว +1

    An app with unit testing ,clean archtecture,dagger,caching

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว

      That exists: th-cam.com/video/8YPXv7xKh2w/w-d-xo.html&ab_channel=PhilippLackner

    • @achalbadgujar9519
      @achalbadgujar9519 2 ปีที่แล้ว

      @@PhilippLackner Thanks lemme chk

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    this video is very good ! Post Phillip flutter lessons on youtube

  • @reinstallx
    @reinstallx 2 ปีที่แล้ว

    Hi, thanks for a great video. May I suggest you make a video-lesson about avoiding if-else inside UI layer. For example how to refactor: if (isVisible) View.VISIBLE else View.INVISIBLE using OOP

  • @adityamaurya9521
    @adityamaurya9521 2 ปีที่แล้ว

    Nice👏👏

  • @m.irtizakhursheed3040
    @m.irtizakhursheed3040 2 ปีที่แล้ว

    Should we need to learn Flutter with Native Android experience.

    • @freshtechtips
      @freshtechtips 2 ปีที่แล้ว

      Yes, when you create flutter project it will create sub projects for android and ios configurations app, you can run native android in flutter with kotlin, swift, object c, java

  • @androiddevo
    @androiddevo 2 ปีที่แล้ว

    Hi , Phillip I think it will be good idea making a tutorial about hms and gms seperation with product flavors 😇

  • @user-sy4lh9hd2s
    @user-sy4lh9hd2s 2 ปีที่แล้ว

    I used to like to use sealed class, but after I found out that Parcelable was not available, I used the model related to Acitivty/Fragment as enum.

    • @pdjeowudjx
      @pdjeowudjx 2 ปีที่แล้ว +1

      Parcelable works for sealed classes

  • @John-qt6qk
    @John-qt6qk 2 ปีที่แล้ว

    So sealed clases is betta for login tho lol thanks I was stuck on this stuff for a bit

    • @deepakbisht4957
      @deepakbisht4957 2 ปีที่แล้ว +1

      Enum restricts the value whereas Sealed classes restrict the type.
      Sealed classes are good when you want to create restricted types that can also have their own state instead of just constant values...

  • @xalikv
    @xalikv 2 ปีที่แล้ว +1

    Post Phillip flutter lessons on youtube

  • @elhadifettah7993
    @elhadifettah7993 2 ปีที่แล้ว

    ⚠️ You should rename the channel as ROMANTIC CODE 🤗

  • @ameentehseen6900
    @ameentehseen6900 2 ปีที่แล้ว

    Hey Philipp, great tutorial I'm kind of stuck in a problem which is how would I be able to use liveData.observe in compose as I need to update a sort of UI. Would love to have a video on it.

  • @Authentic_Reports777
    @Authentic_Reports777 2 ปีที่แล้ว

    I don’t even have soft soft and I’m watcNice tutorialng tNice tutorials sNice tutorialt

  • @xalikv
    @xalikv 2 ปีที่แล้ว +1

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว +1

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว +1

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube

  • @xalikv
    @xalikv 2 ปีที่แล้ว

    Post Phillip flutter lessons on youtube