AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Internal in kotlin5/13/2023 ![]() You build a library and have some abstract class or interface which has multiple different implementations, but you want to make sure the libraries user doesn't add its own implementations (you wan't to be in control of implementation details). Use internal if you wan't your class to be invisible to other modules (you create a library, but certain class in this library shouldn't even be directly compile time usable by libraries users) For example you create a library and you want a class from this library to be used but not extended. So: Use sealed to better control extending something. Also you can directly instantiate an internal class as long as you are doing it in the same module. So you can't even use or instantiate it in another module. Internal class can be used and extended in the same module just like a sealed class, but you can do neither in another module. So sealed class is basically an abstract class which can only be implemented in the same module. This means you can't do this nowhere: val x = M圜lass() You can't instantiate a sealed class directly neither in the same or another module. But you can still use both M圜lass and MyExtensionClass in another module.įor example you can do this in another module: val x: M圜lass = MyExtensionClass() Sealed class M圜lass then you can do this in the same module:īut you can't do the same thing in another module. ![]() Sealed class will be visible in all modules, but extendable only in the same module. ![]()
0 Comments
Read More
Leave a Reply. |