Every Kind of Class in Kotlin
Vložit
- čas přidán 20. 06. 2024
- Kotlin offers many different kinds of classes, and in this video, we're gonna look at all of them - regular classes, nested classes, inner classes, data classes, inline value classes, enum classes, sealed classes, and annotation classes! By the end of this video, you'll understand these different kinds of classes and when to consider using each one.
✨ New to Kotlin? Start your journey here: typealias.com/start/
... or pick up the new Leanpub Edition for offline access and more! book.typealias.com/
🚀 Get the inside scoop on everything I'm working on - join my brand new email newsletter!
Sign up here: newsletter.typealias.com/
00:00 Introduction
00:27 Classes
01:03 Nested Classes
02:41 Inner Classes
03:54 Data Classes
05:19 Value Classes
06:48 Enum Classes
07:42 Sealed Classes
08:39 Annotation Classes
09:45 Wrap-Up - Věda a technologie
Another thing to remember is that in Kotlin, unlike in Java, regular classes are final by default, which means they cannot be subtyped. To make a class open for subtyping, it has to be marked with the "open" modifier.
Yes, thank you for pointing that out, Arthur!
I like your presentation style, very succint and straight to the point
Great, thank you so much!
I don't know where did you come up, but you're the great teacher a lot of us never had. Keep on the good work Dave :)
I appreciate that - thanks so much! I'll keep at it!
The people who haven't subscribed to Dave are probably gonna miss a lot of things 😅
This is a great video, but this video also embodies my biggest gripe with Kotlin. It feels like every potential feature that could maybe be useful in any circumstance was indiscriminately added to the language. Useful, yes, but it gives every single project a different feel and makes it difficult to find consistency, sometimes even within the same project. This is especially felt when using different libraries together.
I can understand that perspective - that's actually how I felt about Groovy. I found it to be a very capable language, but there were so many features and options - even blending dynamic and static typing - that it didn't seem to have a sense of direction, and I suspect even detracted from adoption. I'm hoping Kotlin will avoid _that_ level of feature sprawl - it's a good concern to keep in mind, for sure!
using @Nested inside the test class tip was awesome, most of the time naming test cases is difficult for me but by grouping them using @Nested, naming would be much easier and straight forward. great video by the way
Thanks! Yeah, once I started using `@Nested` in those JUnit 5 tests, I really missed it on the occasions when I had to return to JUnit 4.
@@typealias When do you usually revert back to JUnit4? For me, it's usually when interacting with Roboelectric or such
I usually have a hard time when the examples are contrived, but yours are great : )
Thank you for saying that - I try my best to come up with examples that fit well!
I really like your teaching style, thank you!
Thank you so much! I'm glad you like it!
great video
thanks
Great stuff!
Thank you so much Dave your tutorials are always helpful
Thank you so much! I'm glad you like them!
Well done!
Thank you soo much Dave, you are a gift to all beginners
Hey, you're welcome! It took a while to get around to it, but I appreciate you requesting this one!
Just found your channel. Nice video: cleanly produced, smartly packaged, and thorough without being tedious. Thank you.
Hmm, never used value classes before--talk about boilerplate code!!! That is sooooo awkward, just to catch a parameter error. But that's kotlin for you: tons of extra work in order to keep bad programmers from making mistakes. Give me plain ol C any day (or Lisp).
This is what I was looking for 😊🎉
I wanted to learn kotlin, do I need to learn Java?
Hey Dani! You can certainly learn Kotlin without knowing Java. Sometimes, knowing Java can be a bonus, and could be helpful if you wanted to use a Java library in Kotlin, for example.
You're welcome to start learning Kotlin here: typealias.com/start. In that online book, I don't expect the reader to have any prior Java experience. There are a few occasions where I mention things that might be helpful for someone who is coming from Java, but that's about it. Let me know how it goes as you start learning Kotlin!
For the algo! Thanks youtube to get me here.
Haha, much appreciated! 😁
Object?
Yeah, I decided not to include `object` since it's not quite in the same category... e.g., you can create nested objects and data objects. Might have made a good honorable mention, though!
Freshers can’t understand ur vdos
8:20 I generally use sealed interfaces for that, because they don't store any state and are purely for grouping these sub-classes into a single type.
Unfortunately, the youtrack issue KT-50630/Script-false-positive-NOELSEINWHEN-for-sealed-interface-hierarchy appeared and limits it in when clauses, which is kinda sad at the moment.
I'd argue that sealed classes are to take when it needs to be scoped and have a common property, whereas interfaces should be used as a default.