Kotlin: modifier 'override' is not applicable to 'initializer'

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Kotlin: modifier 'override' is not applicable to 'initializer'



I'm working in a Java project and I'm in charge of migrating some classes to Kotlin. There's a Java abstract class which has the next method:


protected abstract boolean init(params)
...



And this class is being extended in a public class, so I override the method like this:


@Override
protected boolean init(params)
...



So, when trying to migrate this public class to Kotlin,


override protected fun init(params): Boolean



I get the error Kotlin: modifier 'override' is not applicable to 'initializer', because the method's name init is a keyword in Kotlin. I know one way of solving this is changing the name of the method in the abstract class (eg. from init to initialize), but it has tons of usages in the project so I would like to know if there's a better way of solving this before spending time refactoring the whole project.





Welcome to StackOverflow! Your code seems to be syntactically incorrect in some other way. In Kotlin, init is a soft keyword which, in particular, is allowed as a function name. Please post a full code sample (MVCE) that reproduces the error.
– hotkey
Aug 6 at 16:34



init





Besides, you should use the Boolean type in Kotlin rather than boolean.
– hotkey
Aug 6 at 16:35


Boolean


boolean





In Java, if the method you are overriding is public, the override must be public too.
– Andy Turner
Aug 6 at 16:54





What is this params? Can you post full method signatures? I tried to write this but it compiles fine for me.
– Pawel
Aug 6 at 18:33


params




1 Answer
1



Do not use protected keyword. You can use your method like this in public class.


override fun init(): Boolean
...





Welcome to posting on Stack Overflow. Why should we not use protected? Can you please 'edit' this answer with an explanation. How to Answer. Thanks.
– Elletlar
Aug 6 at 16:48





This actually worked, thank you!
– Rodrigo Barrios
Aug 10 at 15:41






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard