Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Practice problems Unit 2 Pathway 1: Android Basics with Compose #149

Open
marceSingularity opened this issue Jul 24, 2024 · 0 comments
Open

Comments

@marceSingularity
Copy link

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 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:

  1. Open Android Studio and create a new Kotlin project.
  2. Create a Kotlin class with a method.
  3. Attempt to subclass this class and override the method without using the open keyword.
  4. 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."

}
no using open
using open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant