Reputation: 28776
I have the following code:
Instead of inject the mutable TokenConfigurationConfig we inject the interface.
public interface TokenAuthenticationConfig {
public fun apiKey() : String
}
@Component
@ConfigurationProperties(prefix = "service.api")
public open class TokenAuthenticationConfigImpl : TokenAuthenticationConfig
{
public var apiKey : String
constructor() {
this.apiKey = ""
}
override fun apiKey(): String
{
return this.apiKey
}
}
It works ok, but just wondering:
Uses of the interface would see the property as immutable, while users of the class would see it as mutable.
Upvotes: 3
Views: 1218
Reputation: 33819
Yes, it definitely is possible to define such an interface and a class.
Any (publicly visible) property x
in Kotlin means a pair of methods getX()
and setX(..)
, generated by the compiler to satisfy the Java convention. That said, it is consistent that you can override getX
in a class and add setX
.
Here is an example:
interface SomethingImmutable {
val Somevar: String
}
class MyClass: SomethingImmutable {
override var Somevar: String = "Initial Value"
}
Upvotes: 3