Override an interface property with a constructor parameter with a different name

I have this code:

class AnyUsernamePersistentNodePath(override val value: String) : AnyPersistenceNodePath { override val key = "username" } 

and

 interface AnyPersistenceNodePath { val key: String val value: String } 

So far so good. Now I want the value parameter in the constructor to be called username instead of value . But obviously, keep the overriding property of the value interface. Is this possible in Kotlin?

I know what I can do:

 class AnyUsernamePersistentNodePath(val username: String) : AnyPersistenceNodePath { override val key = "username" override val value = username } 

but I would like to avoid it.

+7
kotlin
source share
1 answer

You can do what you want by simply removing val from the constructor parameter, so this is a parameter, not a member. Your last class:

 class AnyUsernamePersistentNodePath(username: String) : AnyPersistenceNodePath { override val key = "username" override val value = username } 

You cannot otherwise change the name of what you truly redefine. But you can pass the value that will be assigned to the participant later during construction, as my slightly modified version of your code shows.

+8
source share

All Articles