You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Version: plugins {
id("com.android.application") version "8.2.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
Description:
When using Kotlin in Android Studio, classes and methods are final by default, requiring the open keyword to override them in subclasses. This behavior, while part of Kotlin's design, is not well-documented in the context of Android development in Android Studio's integrated documentation, leading to confusion among developers, especially those new to Kotlin coming from Java backgrounds.
Reproduction Steps:
Open Android Studio and create a new Kotlin project.
Create a Kotlin class with a method.
Attempt to subclass this class and override the method without using the open keyword.
Observe the compilation failure.
Expected Result:
The documentation within Android Studio should clearly state the need for the open keyword to allow method overriding in Kotlin, or the Kotlin plugin should suggest this modification during development.
Actual Result:
The compiler emits an error due to the lack of the open keyword, but no preemptive guidance or clear documentation is provided in the IDE.
Attachments:
Screenshots of the error in Android Studio.
Code snippets showing the attempted overriding without the open keyword.
Conclusion
By clearly articulating the issue following this structured approach, you help ensure that the report is actionable, understandable, and more likely to be addressed effectively by the Google team. This can lead to improvements not only in your development experience but also for the broader community using these tools.
This my code:
package com.example.myapplication
open class Phone(var isScreenLightOn: Boolean = false) {
open fun switchOn() {
isScreenLightOn = true
}
fun switchOff() {
isScreenLightOn = false
}
fun checkPhoneScreenLight() {
val phoneScreenLight = if (isScreenLightOn) "on" else "off"
println("The phone screen's light is $phoneScreenLight.")
}
}
class FoldablePhone(var isFolded: Boolean = false) : Phone() {
override fun switchOn() {
if (!isFolded) {
super.switchOn()
}
}
fun fold() {
isFolded = true
switchOff() // Turn off the screen when the phone is folded
println("Phone folded")
}
fun unfold() {
isFolded = false
println("Phone unfolded")
}
fun trySwitchOn() {
println("Attempting to switch on. Folded: $isFolded")
if (!isFolded) {
switchOn()
} else {
println("Cannot switch on: Phone is folded.")
}
}
}
fun main() {
val foldablePhone = FoldablePhone()
// Unfold the phone and turn it on
foldablePhone.unfold()
foldablePhone.trySwitchOn()
foldablePhone.checkPhoneScreenLight() // Expected: "The phone screen's light is on."
// Fold the phone and check the light
foldablePhone.fold()
foldablePhone.checkPhoneScreenLight() // Expected: "The phone screen's light is off."
}
The text was updated successfully, but these errors were encountered:
Hi, my name is Marcelo Moran. I found this error in the FoldablePhone exercise:
Title: Kotlin 'open' Keyword Requirement Not Clear in Android Studio Documentation
Product: Android Studio and https://play.kotlinlang.org/
Component: Kotlin Plugin
Version: plugins {
id("com.android.application") version "8.2.1" apply false
id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}
Description:
When using Kotlin in Android Studio, classes and methods are
final
by default, requiring theopen
keyword to override them in subclasses. This behavior, while part of Kotlin's design, is not well-documented in the context of Android development in Android Studio's integrated documentation, leading to confusion among developers, especially those new to Kotlin coming from Java backgrounds.Reproduction Steps:
open
keyword.Expected Result:
The documentation within Android Studio should clearly state the need for the
open
keyword to allow method overriding in Kotlin, or the Kotlin plugin should suggest this modification during development.Actual Result:
The compiler emits an error due to the lack of the
open
keyword, but no preemptive guidance or clear documentation is provided in the IDE.Attachments:
open
keyword.Conclusion
By clearly articulating the issue following this structured approach, you help ensure that the report is actionable, understandable, and more likely to be addressed effectively by the Google team. This can lead to improvements not only in your development experience but also for the broader community using these tools.
This my code:
package com.example.myapplication
open class Phone(var isScreenLightOn: Boolean = false) {
open fun switchOn() {
isScreenLightOn = true
}
}
class FoldablePhone(var isFolded: Boolean = false) : Phone() {
override fun switchOn() {
if (!isFolded) {
super.switchOn()
}
}
}
fun main() {
val foldablePhone = FoldablePhone()
}
The text was updated successfully, but these errors were encountered: