hey everyone. I’m trying to understand what are th...
# getting-started
o
hey everyone. I’m trying to understand what are the options for workaround of protected method in interface. I’ve found this one so far:
Copy code
interface Interface {
    fun Interface.protectedMethod()
}
class Implementation : Interface {
    override fun Interface.protectedMethod() = Unit
    
    init {
        protectedMethod()
    }
}
but it doesn’t look fully right. Any other ideas?
r
That's not protected, just scoped. It's easy to access (e.g. using
with
) and provides no guarantee that the
this
in
protectedMethod
is the implementing class. For example: https://pl.kotl.in/YpWSQLFzT
a
you could try messing around with @Deprecation(level = HIDDEN) or just add an underscore prefix to the method to indicate it shouldn’t be used, both of those options are kind of clunky though
d
Have you tried not doing that? 😉 Interfaces members are public by design, it doesn't make sense to have a protected method. Protected members exist for two reasons: 1) super classes need to allow some behavior to be overridden but not exposed. 2) sub-classes need to be able to invoke behavior that isn't exposed. Since interfaces are not super-classes, they don't need protected methods.
1
💡 1