2

I wrote the following code in the IntelliJ IDE as a Kotlin program, and it unbelievably compiled and showed me a runtime error.

As you can see, the compiler forces me to override my var in the Printer class, but it didn't show any error for my val.

open class Printer(override var anString: String) : MyInterface interface MyInterface { var anString: String val anInt: String get() = anInt + 50 } fun main() { println(Printer("string").anInt) } 

Is this intentional or something?

This is my error when I run the code:

> "C:\Program Files\Java\jdk1.8.0_102\bin\java.exe" > "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA > 2019.2.1\lib\idea_rt.jar=52595:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program > Files\Java\jdk1.8.0_102\jre\lib\charsets.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\deploy.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\access-bridge-64.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\cldrdata.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\dnsns.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\jaccess.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\jfxrt.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\localedata.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\nashorn.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\sunec.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\sunjce_provider.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\sunmscapi.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\sunpkcs11.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\ext\zipfs.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\javaws.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\jce.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\jfr.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\jfxswt.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\jsse.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\management-agent.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\plugin.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\resources.jar;C:\Program > Files\Java\jdk1.8.0_102\jre\lib\rt.jar;D:\Work\IntelliJ\KotlinDataTypes\out\production\KotlinDataTypes;C:\Users\Neo\.IntelliJIdea2019.2\config\plugins\Kotlin\kotlinc\lib\kotlin-stdlib.jar;C:\Users\Neo\.IntelliJIdea2019.2\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\Neo\.IntelliJIdea2019.2\config\plugins\Kotlin\kotlinc\lib\kotlin-test.jar;C:\Users\Neo\.IntelliJIdea2019.2\config\plugins\Kotlin\kotlinc\lib\kotlin-stdlib-jdk7.jar;C:\Users\Neo\.IntelliJIdea2019.2\config\plugins\Kotlin\kotlinc\lib\kotlin-stdlib-jdk8.jar" > com.example.accessmodifiers.pack.Test2Kt Exception in thread "main" > java.lang.StackOverflowError at > com.example.accessmodifiers.pack.MyInterface$DefaultImpls.getAnInt(Test2.kt:10) > at com.example.accessmodifiers.pack.Printer.getAnInt(Test2.kt:3) at > com.example.accessmodifiers.pack.MyInterface$DefaultImpls.getAnInt(Test2.kt:10) > at com.example.accessmodifiers.pack.Printer.getAnInt(Test2.kt:3) at > com.example.accessmodifiers.pack.MyInterface$DefaultImpls.getAnInt(Test2.kt:10) > at com.example.accessmodifiers.pack.Printer.getAnInt(Test2.kt:3) at > . > . > . > > Process finished with exit code 1 
1
  • Thank you @4castle for your edit. Commented Sep 15, 2019 at 16:09

2 Answers 2

4

This won't show an error because have defined implementation for the val in the interface. Please keep in mind that whatever you define in an interface is abstract unless you define a concrete implementation, which in your case of val, you did it through get() custom accessor. Also, another point to note that if you don't use field keyword to set/get your implementation, your program will crash because of a recursive function. For example:

get() = field + 50 

Saying anInt + 50, is recursive because the moment you call anInt val, it will call it's get() under the hood, that also calls another get() and so on.

If you have already provided concrete implementation in an abstract class or an interface, the compiler will not force you to override that behaviour but will force you to implement that lack concrete implementation.

Sign up to request clarification or add additional context in comments.

Comments

2

First of all, your anInt declaration is incorrect. It will cause stack overflow error and returns the wrong type.

The compiler doesn't show any error because you provided default behavior for anInt. If you, for example, remove anInt getter, the compiler will force you to declare overridden property in Printer

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.